gpt4 book ai didi

objective-c - 尝试访问特定对象属性时出现无法识别的选择器错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:22:44 27 4
gpt4 key购买 nike

我有一个小问题,对你来说可能很容易。我正在使用核心数据。我有一个实体:Session,它有三个属性:access_token、user_id 和secret。

session .h:

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@interface Session : NSManagedObject

@property (nonatomic) NSString * access_token;
@property (nonatomic) NSNumber * user_id;
@property (nonatomic) NSString * secret;

@end

session .m:

#import "Session.h"

@implementation Session

@dynamic access_token;
@dynamic user_id;
@dynamic secret;

@end

有一个代码,我在其中使用这个实体:

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
Session *newSession = [NSEntityDescription insertNewObjectForEntityForName:@"Session" inManagedObjectContext:managedObjectContext];
if (newSession != nil) {
NSLog(@"access_token: %@", [JSON objectForKey:@"access_token"]);
NSLog(@"user_id: %@", [JSON objectForKey:@"user_id"]);
NSLog(@"secret: %@", [JSON objectForKey:@"secret"]);
newSession.access_token = [JSON objectForKey:@"access_token"];
newSession.user_id = [JSON objectForKey:@"user_id"];
newSession.secret = [JSON objectForKey:@"secret"];
}
NSError *savingError = nil;
if ([managedObjectContext save:&savingError] == YES) {
NSLog(@"Session saved");
} else {
NSLog(@"Session not saved");
}
} failure:nil];

这段代码抛出异常:

newSession.secret = [JSON objectForKey:@"secret"];

MyProject[7678:bc03] -[Session setSecret:]: unrecognized selector sent to instance 0x4e572d0

这是 NSLog:

2012-03-25 15:17:33.987 MyProject[7730:bc03] access_token: <some_access_token>
2012-03-25 15:17:33.988 MyPriject[7730:bc03] user_id: <some_user_id>
2012-03-25 15:17:33.989 MyProject[7730:bc03] secret: <some_secret>

这是堆栈跟踪:

*** Call stack at first throw:
(
0 CoreFoundation 0x010625a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x013e6313 objc_exception_throw + 44
2 CoreFoundation 0x010640bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00fd3966 ___forwarding___ + 966
4 CoreFoundation 0x00fd3522 _CF_forwarding_prep_0 + 50
5 MyProject 0x00004c94 __42-[APIWrapper authWithLogin:andPassword:]_block_invoke_0 + 836
6 MyProject 0x0000d452 __74+[AFJSONRequestOperation JSONRequestOperationWithRequest:success:failure:]_block_invoke_0 + 146
7 MyProject 0x0000e229 __block_global_3 + 41
8 libdispatch_sim.dylib 0x014de289 _dispatch_call_block_and_release + 16
9 libdispatch_sim.dylib 0x014e1833 _dispatch_main_queue_callback_4CF + 312
10 CoreFoundation 0x00fa1589 __CFRunLoopRun + 2521
11 CoreFoundation 0x00fa0840 CFRunLoopRunSpecific + 208
12 CoreFoundation 0x00fa0761 CFRunLoopRunInMode + 97
13 GraphicsServices 0x0153a1c4 GSEventRunModal + 217
14 GraphicsServices 0x0153a289 GSEventRun + 115
15 UIKit 0x000ebc93 UIApplicationMain + 1160
16 MyProject 0x0001b305 main + 181
17 MyProject 0x000020d5 start + 53
)

我真的不明白这里有什么问题。

最佳答案

在您的 .h 文件中,像这样定义您的属性。

@property (nonatomic,retain) NSString * access_token;
@property (nonatomic,retain) NSNumber * user_id;
@property (nonatomic,retain) NSString * secret;

然后在你的.m文件中

@synthesize access_token;
@synthesize user_id;
@synthesize secret;

基本上这会管理您的 getter 和 setter。

关于objective-c - 尝试访问特定对象属性时出现无法识别的选择器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9859792/

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