打印对象 - iPhone 应用程序
我正在尝试打印一个名为 Contact 的对象(我编写的 NSObject 的扩展)。
当我的应用程序首次启动时,我有以下代码:
NSMutableArray *arr = [UserData getGroupNames];
NSLog(@"group names are %@",arr);
for(int i = 0; i < [arr count]; i++) {
NSString *name = [arr objectAtIndex:i];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:name];
NSArray *a = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"name = %@",name);
NSLog(@"array count is %i",[a count]);
for(int i = 0; i < [a count]; i++) {
NSLog(@"on index %i",i);
Contact *c = [a objectAtIndex:i];
NSLog(@"got contact");
if(c == nil)
NSLog(@"it's nil!");
NSLog(@"contact first name = %@",[c getFirst]);
NSLog(@"got contact %@",c);
因此,我得到名为“a”的数组并尝试将其打印出来。好吧,那是行不通的。该程序只是坐在那里,不执行任何操作。它不会出错或退出,它实际上只是停止执行任何操作。
所以我打印了计数,结果是 1。很明显数组中有东西。然后,我从数组中获取第一个联系人,我的控制台打印出“已联系”。
然后,我测试该联系人是否为零。嗯,事实并非如此。
然后,我尝试打印联系人。我的程序停止运行。在控制台中,我只看到“(gdb)”,没有任何打印,也没有任何继续运行。该程序只是坐着。
到底是怎么回事?我只是打印一个联系人(顺便说一下,它设置了一个仅打印 NSString 的描述方法)。
I'm trying to print out an object called Contact (an extension of NSObject I wrote).
When my app first launches, I have the following code:
NSMutableArray *arr = [UserData getGroupNames];
NSLog(@"group names are %@",arr);
for(int i = 0; i < [arr count]; i++) {
NSString *name = [arr objectAtIndex:i];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:name];
NSArray *a = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"name = %@",name);
NSLog(@"array count is %i",[a count]);
for(int i = 0; i < [a count]; i++) {
NSLog(@"on index %i",i);
Contact *c = [a objectAtIndex:i];
NSLog(@"got contact");
if(c == nil)
NSLog(@"it's nil!");
NSLog(@"contact first name = %@",[c getFirst]);
NSLog(@"got contact %@",c);
So, I get my array called "a" and try and print it out. Well, that doesn't work. The program just sits there and doesn't do anything. It doesn't error out or exit out, it literally just stops doing anything.
So I print the count, and that turns out to be 1. So clearly there is something in the array. So then, I grab the first Contact from the array, and my console prints "got contact" fine.
Then, I test to see if that contact is nil. Well, it's not.
Then, I try to print the contact. And my program stops running. In the console I just see "(gdb)" and nothing prints, and nothing continues to run. The program just sits.
What is going on? I am just printing a Contact (which, by the way, has a description method set up which only prints out an NSString).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我敢打赌
c
是一个坏对象。尝试NSStringFromClass([c class])
,但也尝试单步调试器并查看其运行情况。I'd bet
c
is a bad object. TryNSStringFromClass([c class])
, but also try stepping in the debugger and seeing how it goes.