gpt4 book ai didi

objective-c - EXC_BAD_ACCESS 尝试对托管对象中的 NSSET 进行 NSLOG 时

转载 作者:行者123 更新时间:2023-12-03 16:54:41 25 4
gpt4 key购买 nike

我有一个简单的核心数据对象,每当我尝试在 Xcode 中NSLog 其内容时,它都会给我一个 EXC_BAD_ACCESS

托管对象如下所示:

#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 循环来打印消息 NSSet 中的每条消息,它会起作用并证明那里有一些值。

那么,为什么直接打印self.messages会触发EXC_BAD_ACCESS

最佳答案

当涉及到覆盖 NSManagedObject 中的 description 时,文档非常明确:

You can safely invoke the following methods on a fault without causing it to fire: isEqual:, hash, superclass, class, self, isProxy, isKindOfClass:, isMemberOfClass:, conformsToProtocol:, respondsToSelector:, description, managedObjectContext, entity, objectID, isInserted, isUpdated, isDeleted, faultingState, and isFault. Since isEqual and hash do not cause a fault to fire, managed objects can typically be placed in collections without firing a fault. Note, however, that invoking key-value coding methods on the collection object might in turn result in an invocation of valueForKey: on a managed object, which would fire the fault.

Although the description method does not cause a fault to fire, if you implement a custom description method that accesses the object’s persistent properties, this will cause a fault to fire. You are strongly discouraged from overriding description in this way.

您似乎违反了最后一段中的警告。使您的 NSManagedObjectContext 不返回对象作为错误,或使用 valueForKey: 显式触发错误并在尝试访问其描述之前加载结果属性。自行触发的故障对象会导致未定义的行为。

关于objective-c - EXC_BAD_ACCESS 尝试对托管对象中的 NSSET 进行 NSLOG 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15321107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com