Text search though all .xib files in Xcode?

XcodeFull Text-SearchXib

Xcode Problem Overview


This seems like such a basic task, but I'm stumped.

How, in Xcode, do you execute a textual search though (the XML contents of) all the .xib files in a project?

For example, all of our .xib files contain this string on the second line: com.apple.InterfaceBuilder3.CocoaTouch.XIB. So I'd think that searching all project files for that string would return all .xib files, but Xcode insists "0 occurrences". I've double checked that the Project Find Options look correct.

I must be missing something obvious. (Or Xcode is somehow hard-coded to skip .xib files.)

I'm trying to find all the .xib files that reference a particular class (and a text search seems like the most direct way).

Thanks!

Xcode Solutions


Solution 1 - Xcode

What I do is run grep in terminal:

grep -i -r --include=*.xib "TextToFindHere" /PathToSearchHere

Xcode doesn't seem to have an option to search xib files and my attempts to get Spotlight to look at them have been unsuccessful.

Solution 2 - Xcode

Here is my handy command that never fails me. Run it from the project directory in Terminal.

find . -name "*.xib" -exec grep "text to look for" {} ; -print

Solution 3 - Xcode

I realize this is quite old, but I'd like to suggest to those of you who happen to also code other languages on your computer and choose to use Sublime Text 2 (which is wonderful) to do so, just open your iOS project's folder in Sublime and use the find all feature. Very fast, very reliable.

This was what I did to remove unused image resources from one of my larger projects.

~ Happy Coding =]

Solution 4 - Xcode

I may be daft, but using spotlight works great for me on this one.

Solution 5 - Xcode

U may use this function in your bash profile:

function grep_xib {
    grep -i -r --include=*.xib "$1" .
}

Then just call in terminal "grep_xib TEXT_TO_FIND". It's easier.

To create a .bash_profile on your mac follow this steps:

  • Start up Terminal
  • Type "cd ~/" to go to your home folder
  • Type "touch .bash_profile" to create your new file.
  • Edit .bash_profile with your favorite editor (or you can just type "open -e .bash_profile" to open it in TextEdit.
  • Type ". .bash_profile" to reload .bash_profile and update any functions you add.

Solution 6 - Xcode

You can just change the search scope in Xcode by defining a new scope. Go to the search panel and below the search bar there is an icon that has three dots (probably defaulted to "In Workspace"). Click that and under SEARCH SCOPES, click "New Scope". Define it to be "Name" / "ends with" / ".xib" .

Solution 7 - Xcode

grep -i -r --include=*.xib "TextToFindHere" /PathToSearchHere

response: no matches found: --include=*.xib

cd /PathToSearchHere
grep "TextToFindHere" ./ -r | grep ".xib"

this work fine.

Solution 8 - Xcode

Open the project folder in sublime text and search in all files, very easy and convenient.

Solution 9 - Xcode

This works if you use as service....

on run {input, parameters}

 display dialog "XIB Search Text" default answer ""
 set searchtext to text returned of result

 if (length of searchtext) is greater than 0 then
	
   tell application "Xcode" to set theProjectPath to (the project directory of front project)
	
   tell application "Terminal"
		
    set newWin to do script "cd " & theProjectPath & "; " & "rm " & theProjectPath & "/searchXIB.txt 2> /dev/null ; grep -i -r --include=*.xib " & searchtext & " . > searchXIB.txt ; exit"
    activate
		
    repeat while (exists newWin)
     delay 1
    end repeat
		
    tell application "Xcode"
     set doc to open theProjectPath & "/searchXIB.txt"
     activate
    end tell
		
    tell application "System Events"
     keystroke "f" using {command down}
     keystroke searchtext
    end tell
   end tell	
  end if
 return input
end run

Solution 10 - Xcode

I had the same problem and I resolved it using some console commands.

  1. Open Terminal on your Mac

  2. run grep -i -r --include=*.xib "searchtext" /your/project/path command

It will search in the path "/your/project/path" and will print the complete path of all files xib that use the "searchtext".

Solution 11 - Xcode

In the current Xcode version (13), I could not make it work for searching plain text, like stackView in storyboard and xib files.

But it is possible to search for the UI element's id or userLabel, like

<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" 
translatesAutoresizingMaskIntoConstraints="NO" id="kBd-Vg-03c" 
userLabel="Bars Stack View">

you will find this stackView if you provide id kBd-Vg-03c in Xcode find field.

So, combining a couple of the answers regarding find and Terminal in this question, I created a script that returns

all ids of the elements in storyboard and xib files that contain the text that I am searching for.

You call the script from the project's root folder.

#!/usr/bin/env bash

SEARCH=$1
RESULT=""

for ITEM in $(find . \( -name "*.xib" -o -name "*.storyboard" \) -exec grep ${SEARCH} {} \;);
do
    if [[ ${ITEM} =~ "id=" ]]; 
    then
        ITEM=${ITEM#*'"'}; 
        ITEM=${ITEM%'"'*};
        RESULT="${RESULT}${ITEM}|";
    fi
done
RESULT="${RESULT%'|'*}";
echo "${RESULT}"

The result is the combination of all ids with regex's | (or condition). You then copy this result and in Xcode, Find choose Regular Expresion and then paste the script's result string.

You will then get results like this:

Screen shot of Xcode's regex search reuslts

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJon-EricView Question on Stackoverflow
Solution 1 - Xcodeuser467105View Answer on Stackoverflow
Solution 2 - XcodeMoonwalkerView Answer on Stackoverflow
Solution 3 - XcodeRyan CrewsView Answer on Stackoverflow
Solution 4 - XcodeAndrew BennettView Answer on Stackoverflow
Solution 5 - XcodeTedView Answer on Stackoverflow
Solution 6 - XcodeSarah DygertView Answer on Stackoverflow
Solution 7 - XcodeweiminghuaaView Answer on Stackoverflow
Solution 8 - XcodeTaha Selim BebekView Answer on Stackoverflow
Solution 9 - Xcodeort11View Answer on Stackoverflow
Solution 10 - XcodeHimanshu padiaView Answer on Stackoverflow
Solution 11 - XcodeSihad BegovicView Answer on Stackoverflow