56

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!

Jon-Eric
  • 16,977
  • 9
  • 65
  • 97
  • Does this have to be done in Xcode, or can it be done in the terminal? – Reed Olsen Oct 18 '10 at 21:11
  • Ideally, I'd like to know how to do it Xcode so I can click on each result line and have the file open in the Xcode edit pane. If it's impossible in Xcode, I'd like to know that for sure. If that's the case, I'd appreciate alternatives. – Jon-Eric Oct 18 '10 at 22:45

11 Answers11

84

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.

  • 3
    Bingo. Don't run the search in Xcode; run it instead in Terminal.app. To make things simpler, you can drag the file icon from the title bar into Terminal.app and drop it to get the file's path, then just delete till you're back at the project root. – Jeremy W. Sherman Oct 18 '10 at 22:18
  • I've used it but what I get is: **/.DocumentRevisions-V100/PerUID/503/bd/com.apple.documentVersions/14D9CB29-07F2-4670-937F-05188E6D64C6.xib:** which seems to be a revision copy of the xib file, but not the xib file itself.. – aneuryzm Jul 18 '12 at 07:28
  • 2
    Spotlight search may be working now. See http://stackoverflow.com/a/18258064/467105. –  Oct 29 '13 at 20:35
  • 1
    Xcode 6.x now searches xib and storyboard files. See the Interface Builder section in the [Xcode 6 Release Notes](https://developer.apple.com/library/ios/releasenotes/DeveloperTools/RN-Xcode/Chapters/xc6_release_notes.html#//apple_ref/doc/uid/TP40001051-CH4-SW2). –  May 27 '15 at 17:18
  • 1
    I don't know if it is me, but XCode 9 doesn't search in storyboard files :( – darksider Sep 27 '17 at 08:22
  • 4
    Why can't XCode have this simple functionality? – alexr101 Jun 28 '18 at 02:56
16

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

Moonwalker
  • 3,462
  • 4
  • 25
  • 31
9

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 =]

Ryan Crews
  • 3,015
  • 1
  • 32
  • 28
7

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

Andrew Bennett
  • 910
  • 11
  • 11
4

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" .

Sarah Dygert
  • 101
  • 6
  • This is the best answer as it address searching from within Xcode. Note that there is a search scope option to use a specific file extension and setting this to .XIB works well. – Jonathan Mitchell Mar 04 '17 at 16:04
3

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.
Ted
  • 31
  • 2
2

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

Taha Selim Bebek
  • 45
  • 1
  • 8
  • 16
2

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

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

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

this work fine.

weiminghuaa
  • 137
  • 2
  • 11
0

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
ort11
  • 3,359
  • 4
  • 36
  • 69
0

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".

halfer
  • 19,824
  • 17
  • 99
  • 186
Himanshu padia
  • 7,428
  • 1
  • 47
  • 45
  • I tried the above grep search, and the `--include=` with wildcard didn't work until I put the filename-with-wildcard in quotes, e.g. `--include="*.xib"` – Duncan C Feb 03 '23 at 15:27
0

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

Sihad Begovic
  • 1,907
  • 1
  • 31
  • 33