Tat*_*tat 17 arrays programming-languages for-loop objective-c
在Java中,我可以这样做:
int[] abc = new int[10];
for(int i=0; i<abc.length; i++){
abc[i] = i;
}
Run Code Online (Sandbox Code Playgroud)
如何在Objective C中实现类似的功能?
我看到一些使用NSMutableArray的答案,wt和NSArray之间有什么不同?
Cur*_*che 17
尝试这样的事情.
#import <foundation/foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *myArray = [NSMutableArray array];
[myArray addObject:@"first string"];
[myArray addObject:@"second string"];
[myArray addObject:@"third string"];
int count = [myArray count];
NSLog(@"There are %d elements in my array", count);
[pool release];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
循环看起来像:
NSString *element;
int i;
int count;
for (i = 0, count = [myArray count]; i < count; i = i + 1)
{
element = [myArray objectAtIndex:i];
NSLog(@"The element at index %d in the array is: %@", i, element);
}
Run Code Online (Sandbox Code Playgroud)
ken*_*ytm 10
NSMutableArray* abc = [NSMutableArray array];
for (int i = 0; i < 10; ++ i)
[abc addObject:[NSNumber numberWithInt:i]];
return abc;
Run Code Online (Sandbox Code Playgroud)
这里,
-addObject:
在数组的末尾添加一个ObjC.这实际上更类似于C++代码
std::vector<int> abc;
for (int i = 0; i < 10; ++ i)
abc.push_back(i);
return abc;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
29097 次 |
最近记录: |