在 Xcode 中对所有 .xib 文件进行文本搜索?

发布于 2024-09-28 00:52:16 字数 356 浏览 0 评论 0原文

这似乎是一项基本任务,但我很困惑。

在 Xcode 中,如何对项目中的所有 .xib 文件(的 XML 内容)执行文本搜索?

例如,我们所有的 .xib 文件在第二行都包含此字符串:com.apple.InterfaceBuilder3.CocoaTouch.XIB。所以我认为在所有项目文件中搜索该字符串将返回所有 .xib 文件,但 Xcode 坚持“0 次出现”。我已经仔细检查过项目查找选项看起来是否正确。

我一定错过了一些明显的东西。 (或者 Xcode 以某种方式硬编码以跳过 .xib 文件。)

我试图找到引用特定类的所有 .xib 文件(文本搜索似乎是最直接的方法)。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

诗化ㄋ丶相逢 2024-10-05 00:52:17

在sublime text中打开项目文件夹并搜索所有文件,非常简单方便。

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

深爱不及久伴 2024-10-05 00:52:17

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

响应:未找到匹配项:--include=*.xib

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

工作正常。

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

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

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

this work fine.

风吹雨成花 2024-10-05 00:52:17

如果您使用作为服务,这将有效......

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

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
南烟 2024-10-05 00:52:17

我遇到了同样的问题,我使用一些控制台命令解决了它。

  1. 在 Mac 上打开终端

  2. 运行 grep -i -r --include=*.xib "searchtext" /your/project/path 命令

它将在路径“/your/project/path”中搜索并打印完整路径使用“searchtext”的所有文件 xib。

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

怀里藏娇 2024-10-05 00:52:17

在当前的 Xcode 版本 (13) 中,我无法让它用于搜索纯文本,例如 Storyboard 和 xib 文件中的 stackView

但可以搜索 UI 元素的 iduserLabel,就像

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

如果您提供 id kBd,您就会找到这个 stackView - Xcode 查找字段中的 Vg-03c

因此,结合这个问题中有关 findTerminal 的几个答案,我创建了一个脚本,返回

所有 id故事板和 xib 文件中包含我正在搜索的文本的元素

您可以从项目的根文件夹调用该脚本。

#!/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}"

结果是所有 id 与正则表达式的 | (或条件)的组合。
然后复制此结果,并在 Xcode 中,Find 选择Regular Expresion,然后粘贴脚本的结果字符串。

然后您将得到如下结果:

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

回忆那么伤 2024-10-05 00:52:16

我所做的是在终端中运行 grep:

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

Xcode 似乎没有搜索 xib 文件的选项,并且我尝试让 Spotlight 查看它们但没有成功。

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.

昨迟人 2024-10-05 00:52:16

这是我的方便命令,它永远不会让我失望。从终端中的项目目录运行它。

寻找 。 -name "*.xib" -exec grep "要查找的文本" {} \; -打印

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

ぃ双果 2024-10-05 00:52:16

我意识到这已经很老了,但我想建议那些碰巧也在计算机上编写其他语言并选择使用 Sublime Text 2(这非常棒),只需在 Sublime 中打开 iOS 项目的文件夹并使用查找所有功能即可。非常快,非常可靠。

这就是我从一个较大的项目中删除未使用的图像资源时所做的事情。

~ 快乐编码 =]

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

一瞬间的火花 2024-10-05 00:52:16

我可能很愚蠢,但是使用聚光灯对我来说非常有用。

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

2024-10-05 00:52:16

您可以通过定义新范围来更改 Xcode 中的搜索范围。转到搜索面板,搜索栏下方有一个带有三个点的图标(可能默认为“在工作区中”)。单击该按钮,然后在“搜索范围”下单击“新范围”。将其定义为 "Name" / "ends with" / ".xib" 。

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

蓝礼 2024-10-05 00:52:16

您可以在 bash 配置文件中使用此函数:

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

然后只需在终端中调用“grep_xib TEXT_TO_FIND”即可。更容易了。

要在 Mac 上创建 .bash_profile,请按照以下步骤操作:

  • 启动终端
  • 键入“cd ~/”以转到您的主文件夹
  • 键入“touch .bash_profile”以创建新文件。
  • 使用您最喜欢的编辑器编辑 .bash_profile(或者您只需键入“open -e .bash_profile”以在 TextEdit 中打开它。
  • 键入“..bash_profile”以重新加载 .bash_profile 并更新您添加的任何功能。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文