English 中文(简体)
如何更好地处理目标C中的阵列?
原标题:Better way to handle arrays in Objective-C?

请允许我用道歉来形容这个问题。 我对目标C来说是非常新的。 不幸的是,我有一个非常紧凑的工作时间表,需要尽快加快。

下面的法典是行之有效的。 我很想知道,是否有更好的办法处理这一问题。 也许更多的是可可。 整个职能的目的是获得一份具有一定价值的固定职位清单。

由于舒适的原因,我回到了标准阵列。 我最初宣布的国家安全局只是为了测试目的。 既然完全不理解这一措辞,并且仍然把我头部总结在目标C(和C++ I guess)处理变数和点数的方法上,那么将不胜感激。

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

再次感谢任何答复。

最佳回答

做你想要做的事情还不错。 一种方式是:

// 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;
}
问题回答

这可能更为精简(如果是这样的话,在执行时间方面更快):

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]);
}




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签