Text search though all .xib files in Xcode?
Asked Answered
J

11

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!

Janeejaneen answered 18/10, 2010 at 21:3 Comment(2)
Does this have to be done in Xcode, or can it be done in the terminal?Selfreliant
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.Janeejaneen
D
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.

Delphinium answered 18/10, 2010 at 21:13 Comment(6)
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.Ie
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..Spoiler
Spotlight search may be working now. See https://mcmap.net/q/904702/-text-search-though-all-xib-files-in-xcode.Delphinium
Xcode 6.x now searches xib and storyboard files. See the Interface Builder section in the Xcode 6 Release Notes.Delphinium
I don't know if it is me, but XCode 9 doesn't search in storyboard files :(Optimum
Why can't XCode have this simple functionality?Dimpledimwit
A
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

Apish answered 29/1, 2013 at 13:11 Comment(0)
A
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 =]

Archimandrite answered 22/2, 2013 at 21:58 Comment(1)
managed to get it work in VSCode. Xcode makes me frustrating sometimes.Kish
S
7

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

Sydneysydnor answered 15/8, 2013 at 17:20 Comment(2)
Indeed, Spotlight seems to be working now. Perhaps it was fixed in Mountain Lion.Delphinium
Sucks when spotlight works better than xcodes search at finding color set uses!Clutch
P
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" .

Pronominal answered 5/10, 2015 at 21:37 Comment(1)
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.Stirring
D
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.
Dollfuss answered 24/9, 2013 at 11:44 Comment(0)
L
2

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

Lias answered 3/7, 2014 at 18:49 Comment(0)
R
2

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

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

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

this work fine.

Ramonramona answered 11/2, 2019 at 3:25 Comment(0)
D
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
Distinguished answered 24/7, 2013 at 21:36 Comment(1)
Need to document how to do top parameters, note to selfDistinguished
G
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".

Goodard answered 24/2, 2015 at 6:35 Comment(1)
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"Snippet
M
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

Microbe answered 29/10, 2021 at 13:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.