Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 询问Objective-C中的int数组_Iphone_Objective C - Fatal编程技术网

Iphone 询问Objective-C中的int数组

Iphone 询问Objective-C中的int数组,iphone,objective-c,Iphone,Objective C,下面的代码是一个简单的c函数,返回一个int数组。 我想知道如何将其转换为Objective-C private int[] test() { int[] a = new int[2]; a[0] = 1; a[1] = 2; return a; } 位虚拟示例: - (NSArray*) test{ return [NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWi

下面的代码是一个简单的c函数,返回一个int数组。 我想知道如何将其转换为Objective-C

private int[] test()
{
    int[] a = new int[2];
    a[0] = 1;
    a[1] = 2;
    return a;
}
位虚拟示例:

- (NSArray*) test{
  return [NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil];
}
一些注意事项:

NSArray是一种不可变类型-因此,如果要添加/删除值,则应使用NSMutableArray 您只能在NSArray中存储对象,因此需要将它们包装为NSNumber或其他obj-c类型 Obj-c是c的超集,因此如果需要,可以在Obj-c代码中自由使用c数组 位虚拟示例:

- (NSArray*) test{
  return [NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil];
}
一些注意事项:

NSArray是一种不可变类型-因此,如果要添加/删除值,则应使用NSMutableArray 您只能在NSArray中存储对象,因此需要将它们包装为NSNumber或其他obj-c类型 Obj-c是c的超集,因此如果需要,可以在Obj-c代码中自由使用c数组
以下代码应该可以工作:

- (NSArray *)test {
    NSArray *a = [NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil];
    return a;
}
注意,如果我们使用[[NSArray alloc]initWithObjects:blah,blah,nil]创建NSArray;,我们必须在返回数组之前显式自动释放数组,以避免内存泄漏。然而,在本例中,NSArray是使用一个方便的构造函数创建的,因此数组已经为我们自动删除了

第二个问题:

试试这个:

- (NSMutableArray *)test:(int)count {
    NSMutableArray *a = [[NSMutableArray alloc] init];
    for (int i = 0; i < count; i++) {
        [a insertObject:[NSNumber numberWithInt:0] atIndex:i];
    }
    return [a autorelease];
}

以下代码应该可以工作:

- (NSArray *)test {
    NSArray *a = [NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil];
    return a;
}
注意,如果我们使用[[NSArray alloc]initWithObjects:blah,blah,nil]创建NSArray;,我们必须在返回数组之前显式自动释放数组,以避免内存泄漏。然而,在本例中,NSArray是使用一个方便的构造函数创建的,因此数组已经为我们自动删除了

第二个问题:

试试这个:

- (NSMutableArray *)test:(int)count {
    NSMutableArray *a = [[NSMutableArray alloc] init];
    for (int i = 0; i < count; i++) {
        [a insertObject:[NSNumber numberWithInt:0] atIndex:i];
    }
    return [a autorelease];
}

Objective-C中有两种数组:标准C数组和Cocoa NSArray。保留基本整数,但由于C语言的限制,这非常麻烦。必须保留对象,但您可以将整数包装在NSNumbers中,并在需要时将int值取回来

NSArray *array = [NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:500], [NSNumber numberWithInt:12000], nil];

Objective-C中有两种数组:标准C数组和Cocoa NSArray。保留基本整数,但由于C语言的限制,这非常麻烦。必须保留对象,但您可以将整数包装在NSNumbers中,并在需要时将int值取回来

NSArray *array = [NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:500], [NSNumber numberWithInt:12000], nil];

或者,另一种方法是在目标c中允许使用普通c代码:

int a[2]={1, 2};
或在函数中:

int *somefunc(void){
    static int a[2]={1, 2};
    return b;
}

或者,另一种方法是在目标c中允许使用普通c代码:

int a[2]={1, 2};
或在函数中:

int *somefunc(void){
    static int a[2]={1, 2};
    return b;
}