如何通过 Objective-C 中的函数传递数组?
我需要一些帮助。我是 Objective-C 新手,正在尝试学习如何在 Xcode 3.0 中编写 iPhone 应用程序。 我试图通过函数传递我的数组,但很困惑。感谢您的帮助。 这是我的代码:
#import <Foundation/Foundation.h>
void add(int x) {
NSLog(@"%i + 2 = %i", x, x + 2);
}
int main (int argc, const char * argv[]) {
NSNumber *arrayNumber = [[NSNumber alloc] initWithInt:5];
NSNumber *arrayNumber2 = [[NSNumber alloc] initWithInt:9];
NSMutableArray *arrayNumbers = [[NSMutableArray alloc] initWithCapacity:1];
[arrayNumbers addObject:arrayNumber];
[arrayNumbers addObject:arrayNumber2];
NSLog(@"object at index 0 = %i", [[arrayNumbers objectAtIndex:0] intValue]);
NSLog(@"object at index 1 = %i", [[arrayNumbers objectAtIndex:1] intValue]);
for(NSNumber *answer in arrayNumbers) {
add();
}
return 0;
}
我对 add() 部分的括号中的内容感到困惑:
for(NSNumber *answer in arrayNumbers) {
add();
}
I need some help. I am new to Objective-C and trying to learn how to program iPhone Applications in Xcode 3.0.
I am trying to pass my array through a function and am confused. Thank you for your help.
Here is my code:
#import <Foundation/Foundation.h>
void add(int x) {
NSLog(@"%i + 2 = %i", x, x + 2);
}
int main (int argc, const char * argv[]) {
NSNumber *arrayNumber = [[NSNumber alloc] initWithInt:5];
NSNumber *arrayNumber2 = [[NSNumber alloc] initWithInt:9];
NSMutableArray *arrayNumbers = [[NSMutableArray alloc] initWithCapacity:1];
[arrayNumbers addObject:arrayNumber];
[arrayNumbers addObject:arrayNumber2];
NSLog(@"object at index 0 = %i", [[arrayNumbers objectAtIndex:0] intValue]);
NSLog(@"object at index 1 = %i", [[arrayNumbers objectAtIndex:1] intValue]);
for(NSNumber *answer in arrayNumbers) {
add();
}
return 0;
}
I am confused by what to put in the parentheses in the part with add():
for(NSNumber *answer in arrayNumbers) {
add();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
-intValue
像这样:You need to unbox the
NSNumber
using-intValue
like this: