Why is this NULL?

…衆ロ難τιáo~ 提交于 2019-12-25 02:07:04

问题


Can anyone tell me why this is NULL ?

So Array1 is NSMutable, and has an NSString object "UserName" at index 0;

NSLog (@"Contents of array1 %@", [array1 objectAtIndex:0]); //prints UserName

Now , I do this...

array2 belongs to another class. I reference the class, import .h file, and add property and synthesize it.

[object2.array2  addObject: array1]; //array2 is NSMutable properly initialized in it's respective class in the init method

NSLog (@"Contents of array2 %@", [object2.array2 objectAtIndex:0]); //prints (null)

tempArray = [[NSMutableArray alloc] init];
tempArray = [object2.array2 objectAtIndex:0];
NSLog (@"%@", [tempArray objectAtIndex:0]); // Prints (null) // Should be UserName

Adding more information:

object2 is an object of another class (which is a subclass of NSObject), and object2 contains array2 as it's property.

object2 is nil. WHY ? HOW ??


回答1:


NSLog (@"Contents of array2 %@", [object2.array2 objectAtIndex:0]); //prints NULL

If that 'prints NULL', then it is for one of four reasons:

  • object2 is nil
  • object2.array2 returns nil
  • the object at index 0 is the string "NULL"
  • the object at index 0's description method returns the string "NULL"

Which is it?


object 2 is nil. I wonder why.

Most likely because you didn't initialize it in the first place. Show your initialization code and also drop a log statement or breakpoint and make sure it is actually being executed.



来源:https://stackoverflow.com/questions/6376474/why-is-this-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!