如何根据某些字符分隔文件中的 NSString?
我需要根据字符“~”分隔应用程序打开的文件。例如,如果我保存一个包含字符串“test~test2”的文本文件,我需要它将字符串拆分为两个 NSString:(@“test”和@“test2”)。我该怎么做?
我更喜欢将字符串加载到 NSTextView 中并让它查看单个字母,如下所示:
for (int integerA = 0; integerA < [textViewString length]; integerA ++) {
[textViewString selectRange(integerA, integerA)];
if ([textViewString selectedRange] == @"~") {
//then split the string
}
}
唯一的问题是没有“selectRange”或“selectedRange”方法。其他方法完全没问题,但我希望能够让它一一进行检查并将它们添加到正确的字符串中,因为我可能需要将文本文件中的字符串拆分为多达十五个字符串。
非常感谢任何帮助!
编辑:解决该问题后,出现了一个新问题:我需要存储 12 个值,但 NSArray 说了以下内容。
HIToolbox:忽略在 Carbon 事件调度内引发的异常 '* -[NSCFArray objectAtIndex:]: index (7)超出边界(7)'
我该怎么解决这个问题?
I need to separate the files my application opens based on the character "~". For example, if I save a text file with the string "test~test2" I need it to split the string into two NSStrings: (@"test" and @"test2"). How can I do this?
I would prefer to load the string into an NSTextView and have it look at individual letters, like so:
for (int integerA = 0; integerA < [textViewString length]; integerA ++) {
[textViewString selectRange(integerA, integerA)];
if ([textViewString selectedRange] == @"~") {
//then split the string
}
}
The only problem is that there is no "selectRange" or "selectedRange" method. Other ways are perfectly fine, but I would like to be able to make it go through one-by-one and add them to the correct string, because I may need to split the string in the text file into as many as fifteen strings.
Any help is very much appreciated!
EDIT: After solving that problem, a new problem appeared: I need 12 values stored, but the NSArray says the following.
HIToolbox: ignoring exception '* -[NSCFArray objectAtIndex:]: index (7) beyond bounds (7)' that raised inside Carbon event dispatch
What can I do to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
?
?
Wevah 已经回答了你的问题,但我想我应该为你指出其他一些事情:
提取子字符串的方法称为
substringWithRange:
,这采用NSRange
结构作为参数。NSRange
结构的成员是location
和length
。当您想要提取子字符串时,初始化NSRange
结构并将其传递给substringWithRange:
以提取这些字符:不要简单地使用此方法来逐一提取字符,因为还有其他方法可用。
在 Objective-C 中,
==
不能用于检查两个字符串是否相等,而必须使用isEqualToString:
方法,如下所示:Wevah has already answered your question, but I thought I would point out a couple of other things for you:
The method for extracting a substring is called
substringWithRange:
, this takes anNSRange
structure as an argument. The members of anNSRange
struct arelocation
andlength
. When you want to extract a substring, initialise anNSRange
struct and pass it tosubstringWithRange:
to extract those characters:Don't use this method simply to extract characters one-by-one though, for that there are other methods available.
In Objective-C,
==
cannot be used to check whether two strings are equal, instead, you must use theisEqualToString:
method, like this: