NSStringCompareOptions 与 NSDiacriticInsensitiveSearch;

发布于 2024-11-06 18:05:19 字数 682 浏览 2 评论 0原文

我正在尝试比较两个字符串

  • String1 来自文件。
  • String2 在 NSArray (预定义列表)中,

我想比较这两个字符串,如果匹配,可能会执行 NSLog

NSStringCompareOptions compareOptions = NSDiacriticInsensitiveSearch;
NSArray* countryIndex = [[NSArray alloc] initWithObjects:@"alpha",
                                                         @"beta",
                                                         @"gamma",
                                                         nil];

for (NSString* element in countryIndex) {
    NSComparisonResult result = [(NSString *)country compare:element options:compareOptions];
}

所以我很困惑结果是什么? (数量、类别等)

I am trying to compare two strings

  • String1 is from a file.
  • String2 is in an NSArray (predefined list)

I want to compare the two strings and if it's a match, maybe do an NSLog

NSStringCompareOptions compareOptions = NSDiacriticInsensitiveSearch;
NSArray* countryIndex = [[NSArray alloc] initWithObjects:@"alpha",
                                                         @"beta",
                                                         @"gamma",
                                                         nil];

for (NSString* element in countryIndex) {
    NSComparisonResult result = [(NSString *)country compare:element options:compareOptions];
}

So I'm very confused at what result is? (number, class, etc)

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

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

发布评论

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

评论(1

小草泠泠 2024-11-13 18:05:19

请在 此处查看 Apple 文档

如果您搜索“NSComparisonResult”,您将看到它是一个枚举,其中包含可用于检查比较操作结果的常量。

以下是链接文档中的一个简短片段:

NSComparisonResult
These constants are used to indicate how items in a request are ordered.

enum {
   NSOrderedAscending = -1,
   NSOrderedSame,
   NSOrderedDescending
};
typedef NSInteger NSComparisonResult;

例如,为了在您的代码你可以执行以下操作:

NSStringCompareOptions  compareOptions = NSDiacriticInsensitiveSearch;
NSArray* countryIndex = [[NSArray alloc] initWithObjects:@"alpha", @"beta", @"gamma' nil];

for (NSString* element in countryIndex) {
    NSInteger result = [(NSString *)country compare:element options:compareOptions];
    if(NSOrderedAscending == result) {
        // Do something here...
    }
    else if (NSOrderedSame == result) {
        // Do another thing here if they match...
    }
    else {
        // Try something else...
    }
}

Check the Apple documentation available here.

If you do a search for 'NSComparisonResult' you will see it's an enum, containing constants you can use to check what the comparison operation resulted in.

Here's a brief snippet from the linked document:

NSComparisonResult
These constants are used to indicate how items in a request are ordered.

enum {
   NSOrderedAscending = -1,
   NSOrderedSame,
   NSOrderedDescending
};
typedef NSInteger NSComparisonResult;

So for instance, in order to use it in your code you could do the following:

NSStringCompareOptions  compareOptions = NSDiacriticInsensitiveSearch;
NSArray* countryIndex = [[NSArray alloc] initWithObjects:@"alpha", @"beta", @"gamma' nil];

for (NSString* element in countryIndex) {
    NSInteger result = [(NSString *)country compare:element options:compareOptions];
    if(NSOrderedAscending == result) {
        // Do something here...
    }
    else if (NSOrderedSame == result) {
        // Do another thing here if they match...
    }
    else {
        // Try something else...
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文