开发者

Text search though all .xib files in Xcode?

开发者 https://www.devze.com 2023-01-21 09:14 出处:网络
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?

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 开发者_StackOverflow社区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!


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.


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


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


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


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.


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


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


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

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

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

this work fine.


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


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


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:

Text search though all .xib files in Xcode?

0

精彩评论

暂无评论...
验证码 换一张
取 消