在Objective-C的一个int数组实例变量的值怪 [英] Strange values in an int array instance variable in Objective-C

查看:137
本文介绍了在Objective-C的一个int数组实例变量的值怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找类似​​的话题,但到目前为止没有运气,所以这里有云:

I looked for similar topics but no luck so far, so here it goes:

在一个Objective-C I类声明的 INT 指针实例变量持有 INT 的数组:

In an Objective-C class I declared an int pointer instance variable to hold an array of int:

@interface MyList : NSObject {
    int index;      // A simple int to hold an index reference
    NSString *name; // The name of the list
    int *bookList;  // A pointer to an int array that holds a list of numbers
}

@property (nonatomic) int index;
@property (nonatomic, copy) NSString *name;
@property (nonatomic) int *bookList;

@end

我测试了这个原则和包含的实例变量的所有数据正确存放,由的NSLog 报表显示:

I tested this as follows and all data contained in the instance variables was correctly stored and displayed by the NSLog statements:

MyList *aList = [[MyList alloc] init];
[aList setIndex:1];
[aList setName:@"ListOne"];
[aList setBookList:(int []){1, 2, 3, 0}];

NSLog(@"Show MyList object's data after object is populated");
NSLog(@"[%d]: %@", aList.index, aList.name);
for (int i = 0; i < 4; i++) {
    NSLog(@"bookList[%d] = %d", i, aList.bookList[i]);
}

然而的,当我把这个对象作为参数的方法和我尝试打印 INT 数组的内容,我得到奇怪的数字,并从该方法返回后同样的情况:

However, when I send this object as an argument to a method and I try to print the contents of the int array, I get strange numbers, and the same happens after returning from the method:

-(void)displayMyList:(MyList *)theList {
    NSLog(@"Show MyList object's data in displayMyList method");
    NSLog(@"[%d]: %@", theList.index, theList.name);
    for (int i = 0; i < 4; i++) {
        NSLog(@"bookList[%d] = %d", i, theList.bookList[i]);
    }
}

我不知道这有什么错我的测试code,因为指数的价值观和名称当对象被发送到 displayMyList实例变量没有得到改变:方法。我调试一步一步的指针 INT 阵列指向同一个地址的所有的时间,所以它似乎有一个副作用的地方,正在改变阵列的价值观或者我没有变内存是如何放置这种类型的指针为 INT 阵列。也许这只是一些指针运算,我没有得到正确的,因为我还没有见过这种在任何iPhone编程的书我有。

I don't know what's wrong with my test code, as the values of the index and name instance variables don't get changed when the object is sent to the displayMyList: method. I debugged step-by-step and the pointer to the int array points to the same address all the time, so it seems there's a side-effect somewhere that's changing the array's values, or I'm not getting how memory is placed for this type of pointers to int arrays. Maybe it's just some pointer arithmetic I'm not getting right, because I haven't seen this in any of the iPhone programming books I have.

我想实施 INT 数组,因为我用它做数学题是非常简单的,它似乎使用的NSArray 是矫枉过正(如果此方法行不通,我可以随时与的NSArray 去,虽然)。

I wanted to implement the int array because the math I do with it is really simple and it seemed using an NSArray was overkill (if this approach doesn't work I can always go with an NSArray, though).

推荐答案

您的问题就在这里:

[aList setBookList:(int[]){1,2,3,0}];

一个堆栈分配基准的范围仅限于周边的功能,这就是为什么你在不同的范围内获得垃圾值,因为在其被分配的堆栈不再使用。

The scope of a stack-allocated reference is limited only to the surrounding function, that's why you're getting garbage values in a different scope, because the stack in which it was allocated is no longer in use.

您需要的malloc 名单 INT s到这个参考推入堆,就像这样:

You need to malloc this list of ints to push this reference onto the heap, like so:

int sourceList[] = { 1, 2, 3, 0 }; 
int *bookList = malloc(sizeof sourceList); 

memcpy(aList->bookList, sourceList, sizeof sourceList);

既然你叫的malloc ,你最终必须通过调用免费中放弃了阵列所占用的内存你的 -dealloc 方法。

Since you called malloc, you must eventually relinquish the memory occupied by the "array" by calling free in your -dealloc method.

作为替代方案,你可以使用的NSArray 的NSNumber 的对象,像这样的:

As an alternative, you could use an NSArray of NSNumber objects, like so:

@property (nonatomic, strong) NSArray *bookList;

//...

#define NUMINT(x) [NSNumber numberWithInt:x]

NSArray *bookList = [NSArray arrayWithObjects:NUMINT(1), NUMINT(2), NUMINT(3), NUMINT(0), nil];
[aList setBookList:bookList];

这篇关于在Objective-C的一个int数组实例变量的值怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆