为什么是NULL? [英] Why is this NULL?

查看:69
本文介绍了为什么是NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我为什么这是NULL?

因此Array1是NSMutable,并且在索引0处具有NSString对象"UserName";

  NSLog(@"array1%@的内容",[array1 objectAtIndex:0]);//打印用户名 

现在,我这样做...

array2属于另一个类.我引用该类,导入.h文件,并添加属性并对其进行合成.

  [object2.array2 addObject:array1];//array2是NSMutable,可以在init方法的相应类中正确初始化NSLog(@"array2%@的内容,[object2.array2 objectAtIndex:0]);//打印(空)tempArray = [[NSMutableArray alloc] init];tempArray = [object2.array2 objectAtIndex:0];NSLog(@%@",[tempArray objectAtIndex:0]);//打印(空)//应该是UserName 

添加更多信息:

object2 是另一个类的对象(这是 NSObject 的子类),而object2包含array2作为其属性.

object2为零.为什么 ?如何??

解决方案

  NSLog(@"array2的内容%@",[object2.array2 objectAtIndex:0]);//输出NULL 

如果打印为NULL",则是以下四个原因之一:

  • object2 为零
  • object2.array2 返回nil
  • 索引为0的对象是字符串"NULL"
  • 索引0的 description 方法中的对象返回字符串"NULL"

是哪个?


对象2为零.我不知道为什么.

最有可能是因为您没有首先对其进行初始化.显示您的初始化代码,并删除一条日志语句或断点,并确保它实际上已在执行.

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 ??

解决方案

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.

这篇关于为什么是NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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