【问题标题】:Better way to handle arrays in Objective-C?在 Objective-C 中处理数组的更好方法?
【发布时间】:2012-05-15 23:14:05
【问题描述】:

请让我以道歉的方式开始这个问题。我对 Objective C 很陌生。不幸的是,我的工作项目时间很紧,需要尽快跟上进度。

下面的代码有效。我只是想知道是否有更好的方法来处理这个......也许更可可的东西。该函数的全部目的是获取字符串中具有特定值的位置的有序列表。

出于舒适的原因,我选择了标准阵列。我最初声明的 NSMutableString 仅用于测试目的。作为这门语言的一个完全的菜鸟,并且仍然围绕着 Objective C(我猜是 C++)处理变量和指针的方式,任何和所有的提示、提示、指针都将不胜感激。

NSMutableString *mut = [[NSMutableString alloc] initWithString:@"This is a test of the emergency broadcast system.  This is only a test."];

NSMutableArray *tList = [NSMutableArray arrayWithArray:[mut componentsSeparatedByString:@" "]];

int dump[(tList.count-1)];
int dpCount=0;
NSUInteger length = [mut length];
NSRange myRange = NSMakeRange(0, length);

while (myRange.location != NSNotFound) {

    myRange = [mut rangeOfString:@" " options: 0 range:myRange];

    if (myRange.location != NSNotFound) {
        dump[dpCount]=myRange.location;
        ++dpCount;
        myRange = NSMakeRange((myRange.location+myRange.length), (length - (myRange.location+myRange.length)));
    }
}

for (int i=0; i < dpCount; ++i) {
    //Going to do something with these values in the future... they MUST be in order.
    NSLog(@"Dumping positions in order: %i",dump[i]);
}
text2.text = mut;
[mut release];

再次感谢您的任何回复。

【问题讨论】:

  • Sagan,一般来说,Stack Overflow 并不是为了提供代码审查或类似的东西。它适用于有特定答案的问题。查看FAQ 了解更多信息...话虽如此,总的来说,我认为一些指南对于理解这类东西很有用。有很棒的书籍和在线资源可以帮助您。目前我真的没有看到您的代码有任何重大问题,因此可能没有什么需要注意的。
  • 要点...并为浪费的问题道歉。似乎我可能在那里遗漏了一些明显的东西。我会尝试只发布我的“为什么这不起作用!!!”未来的问题。再次感谢您的宝贵时间。
  • 你能更准确地解释一下这个函数应该做什么吗?您将单词分成tList,但从不使用tList,那么您似乎只是在遍历字符串试图找到空格字符的所有位置?
  • 首先,似乎没有什么好的理由在这里使用可变字符串和数组;如果您不想更改内容,那么您只是无缘无故地降低性能和安全性。其次,正如 dreamlax 所暗示的,您的循环似乎正在计算与 componentsSeparatedByString: 已经为您计算的完全相同的信息。一旦你已经调用了 componentsSeparatedByString:,你就可以得到位置,例如,将所有组件的长度相加。
  • @dreamlax - 我使用 tList 的唯一原因是获取启动转储数组的大小。

标签: iphone objective-c arrays nsmutablestring


【解决方案1】:

没有很好的方法来做你想做的事。这是一种方法:

// locations will be an NSArray of NSNumbers -- 
// each NSNumber containing one location of the substring):
NSMutableArray *locations = [NSMutableArray new];
NSRange searchRange = NSMakeRange(0,string.length);
NSRange foundRange;
while (searchRange.location < string.length) {
    searchRange.length = string.length-searchRange.location;
    foundRange = [string rangeOfString:substring options:nil range:searchRange];
    if(foundRange.location == NSNotFound) break;
    [locations addObject:[NSNumber numberWithInt:searchRange.location]];
    searchRange.location = foundRange.location+foundRange.length;
}

【讨论】:

  • 谢谢。我使用标准数组的全部原因是因为某种原因我无法将 location 属性填入 NSNumber ......它一直给我一个指针错误。你的方法奏效了,我以后可以列举出来。
【解决方案2】:

这可能会更加精简(如果重要的话,执行时间会更快):

const char *p = "This is a test of the emergency broadcast system.  This is only a test.";
NSMutableArray *positions = [NSMutableArray array];

for (int i = 0; *p; p++, i++)
{
    if (*p == ' ') {
        [positions addObject:[NSNumber numberWithInt:i]];
    }
}

for (int j = 0; j < [positions count]; j++) {
    NSLog(@"position %d: %@", j + 1, [positions objectAtIndex:j]);
}

【讨论】:

  • 三元运算符不是必须的,你说的是“如果i为0则将0放入数组中,否则如果i不为0则将i放入数组中数组”。
  • 谢谢。外观优雅的解决方案。看来我可能需要退后一步,也参加 C++ 速成课程。
  • @dreamlax - 哇!我在发布之前调整了解决方案代码并忘记将其删除。谢谢,我会更新答案。
  • @Sagan - 那只是普通的旧 ANSI C。C++ 是一门独立的语言;你不需要学习它来进行 Objective-C 开发。然而,ANSI C 是 Objective-C 语言的一部分,因此您确实想学习。
猜你喜欢
  • 1970-01-01
  • 2016-07-22
  • 1970-01-01
  • 2010-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-14
  • 1970-01-01
相关资源
最近更新 更多