如何根据某些字符分隔文件中的 NSString?

发布于 2024-10-29 11:24:10 字数 717 浏览 4 评论 0原文

我需要根据字符“~”分隔应用程序打开的文件。例如,如果我保存一个包含字符串“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 技术交流群。

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

发布评论

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

评论(2

白芷 2024-11-05 11:24:10
NSArray *strings = [fileContentsAsString componentsSeparatedByString:@"~"];

NSArray *strings = [fileContentsAsString componentsSeparatedByString:@"~"];

?

只是在用心讲痛 2024-11-05 11:24:10

Wevah 已经回答了你的问题,但我想我应该为你指出其他一些事情:

  1. 提取子字符串的方法称为 substringWithRange:,这采用 NSRange 结构作为参数。 NSRange 结构的成员是 locationlength。当您想要提取子字符串时,初始化 NSRange 结构并将其传递给 substringWithRange: 以提取这些字符:

    NSRange myRange = { .location = integerA, .length = 1 };
    NSString *mySubstr = [textViewString substringWithRange:myRange];
    

    不要简单地使用此方法来逐一提取字符,因为还有其他方法可用。

  2. 在 Objective-C 中,== 不能用于检查两个字符串是否相等,而必须使用 isEqualToString: 方法,如下所示:

    if ([mySubstr isEqualToString:@"~"])
    {
        // ...
    }
    

Wevah has already answered your question, but I thought I would point out a couple of other things for you:

  1. The method for extracting a substring is called substringWithRange:, this takes an NSRange structure as an argument. The members of an NSRange struct are location and length. When you want to extract a substring, initialise an NSRange struct and pass it to substringWithRange: to extract those characters:

    NSRange myRange = { .location = integerA, .length = 1 };
    NSString *mySubstr = [textViewString substringWithRange:myRange];
    

    Don't use this method simply to extract characters one-by-one though, for that there are other methods available.

  2. In Objective-C, == cannot be used to check whether two strings are equal, instead, you must use the isEqualToString: method, like this:

    if ([mySubstr isEqualToString:@"~"])
    {
        // ...
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文