菜鸟教程小白 发表于 2022-12-11 22:18:33

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


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

<p> session .h:
</p>

<pre class="lang-c prettyprint-override"><code>#import &lt;Foundation/Foundation.h&gt;
#import &lt;CoreData/CoreData.h&gt;

@interface Session : NSManagedObject

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

@end
</code></pre>

<p> session .m:</p>

<pre class="lang-c prettyprint-override"><code>#import &#34;Session.h&#34;

@implementation Session

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

@end
</code></pre>

<p>有一个代码,我在使用这个实体:</p>

<pre class="lang-c prettyprint-override"><code>AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    Session *newSession = ;
    if (newSession != nil) {
      NSLog(@&#34;access_token: %@&#34;, );
      NSLog(@&#34;user_id: %@&#34;, );
      NSLog(@&#34;secret: %@&#34;, );
      newSession.access_token = ;
      newSession.user_id = ;
      newSession.secret = ;
    }
    NSError *savingError = nil;
    if ( == YES) {
      NSLog(@&#34;Session saved&#34;);
    } else {
      NSLog(@&#34;Session not saved&#34;);
    }
} failure:nil];
</code></pre>

<p>此代码异常:</p>

<pre class="lang-c prettyprint-override"><code>newSession.secret = ;

MyProject -: unrecognized selector sent to instance 0x4e572d0
</code></pre>

<p>这是 NSLog:</p>

<pre class="lang-c prettyprint-override"><code>2012-03-25 15:17:33.987 MyProject access_token: &lt;some_access_token&gt;
2012-03-25 15:17:33.988 MyPriject user_id: &lt;some_user_id&gt;
2012-03-25 15:17:33.989 MyProject secret: &lt;some_secret&gt;
</code></pre>

<p>这是堆栈跟踪:</p>

<pre class="lang-c prettyprint-override"><code>*** Call stack at first throw:
(
0   CoreFoundation                      0x010625a9 __exceptionPreprocess + 185
1   libobjc.A.dylib                     0x013e6313 objc_exception_throw + 44
2   CoreFoundation                      0x010640bb - + 187
3   CoreFoundation                      0x00fd3966 ___forwarding___ + 966
4   CoreFoundation                      0x00fd3522 _CF_forwarding_prep_0 + 50
5   MyProject                           0x00004c94 __42-_block_invoke_0 + 836
6   MyProject                           0x0000d452 __74+_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
10CoreFoundation                      0x00fa1589 __CFRunLoopRun + 2521
11CoreFoundation                      0x00fa0840 CFRunLoopRunSpecific + 208
12CoreFoundation                      0x00fa0761 CFRunLoopRunInMode + 97
13GraphicsServices                  0x0153a1c4 GSEventRunModal + 217
14GraphicsServices                  0x0153a289 GSEventRun + 115
15UIKit                               0x000ebc93 UIApplicationMain + 1160
16MyProject                           0x0001b305 main + 181
17MyProject                           0x000020d5 start + 53
)
</code></pre>

<p>我真的不明白这里有什么问题。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在您的 .h 文件中定义您的属性,如下所示。</p>

<pre><code>@property (nonatomic,retain) NSString * access_token;
@property (nonatomic,retain) NSNumber * user_id;
@property (nonatomic,retain) NSString * secret;
</code></pre>

<p>然后在你的 .m 文件中</p>

<pre><code>@synthesize access_token;
@synthesize user_id;
@synthesize secret;
</code></pre>

<p>这基本上管理你的 getter 和 setter。</p></p>
                                   
                                                <p style="font-size: 20px;">关于objective-c - 尝试访问特定对象属性时出现无法识别的选择器错误,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/9859792/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/9859792/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: objective-c - 尝试访问特定对象属性时出现无法识别的选择器错误