我有一个简单的核心数据对象,每当我在 Xcode 中EXC_BAD_ACCESS
尝试它的内容时,它都会给我一个。NSLog
托管对象如下所示:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class ChatFriend, ChatMessage;
@interface ChatThread : NSManagedObject
@property (nonatomic, retain) NSString * threadId;
@property (nonatomic, retain) NSDate * timestamp;
@property (nonatomic, retain) NSSet *friends;
@property (nonatomic, retain) NSSet *messages;
@end
@interface ChatThread (CoreDataGeneratedAccessors)
- (void)addFriendsObject:(ChatFriend *)value;
- (void)removeFriendsObject:(ChatFriend *)value;
- (void)addFriends:(NSSet *)values;
- (void)removeFriends:(NSSet *)values;
- (void)addMessagesObject:(ChatMessage *)value;
- (void)removeMessagesObject:(ChatMessage *)value;
- (void)addMessages:(NSSet *)values;
- (void)removeMessages:(NSSet *)values;
@end
这是.m
文件:
#import "ChatThread.h"
#import "ChatFriend.h"
#import "ChatMessage.h"
@implementation ChatThread
@dynamic threadId;
@dynamic timestamp;
@dynamic friends;
@dynamic messages;
- (NSString*) description
{
/* This can print out the contents of self.messages correctly
for(ChatMessage *message in self.messages)
{
Log(@"ChatMessage - %@", message);
}
*/
// using %@ to print out self.messages directly, will fail:
return [NSString stringWithFormat:@"ChatThread - threadId:%@, messages count:%d, messages:%@", self.threadId, self.messages.count, self.messages];
}
@end
我注意到,如果我尝试打印出该stringWithFormat
行中 self.messages 的内容,EXC_BAD_ACCESS
将会发生。如果我删除 self.messages,只打印出self.messages.count
,它可以工作。如果我手动执行 for 循环以打印出 messages 中的每条消息NSSet
,它就可以工作并且证明那里有一些值。
那么,为什么直接打印 self.messages 会触发EXC_BAD_ACCESS
呢?