IOS create object

I want to create an object called Note. Then I want to assign some values ​​to it and display them in the console to make sure that all this works correctly.

Note.h #import <Foundation/Foundation.h> @interface Note : NSObject { NSNumber *position; NSString *syllable; } @property(nonatomic, retain)NSNumber *position; @property(nonatomic, retain)NSString *syllable; @end Note.m #import "Note.h" @implementation Note @dynamic position; @dynamic syllable; @end 

After that, in another class, I want to assign some values ​​to the fields.

 -(void) configNote:(Note*)not { not = [Note alloc]; [not setPosition:[NSNumber numberWithInt:2]]; [not setSyllable:@"Twin-"]; } ..... Note *note; [self configNote:note]; NSLog(@" pos : %d syl : %@ ",[[note position] integerValue],[note syllable]); 

I tried using @synthesize instead of @dynamic , but nothing changes anyway. The cause of the error is: -[Note setPosition:]: unrecognized selector sent to instance 0x733d060

+4
source share
4 answers

init your note and use your own initialization method to initialize ivars and change dynamic synthesis and to better use this init to initialize your varibales

 - (id)initWithPosition:(NSNumber*)pos andSyllable:(NSString*)syl { self = [super init]; if (self) { self.position = pos; self.syllable= syl; } return self; } 
+4
source

Since your question is quite simple and fundamental, you can check out this tutorial at cocoadevcentral. This is very well done and teaches you some basics when working with objects.

+2
source

If you use ARC, try "strong" instead of "retain" .

0
source

Your problem is that you pass the pointer of the Note object to configNote: by value, not by reference. Inside configNote: when you set not to the newly selected Note object, you actually set the local variable not this value. What your NSLog call outside of your configNote: object actually receives is the original note value before the configObject: call.

To get what you think you want, either more highlighting the Note object outside the configObject: method, or pass the pointer of the Note object to configObject: by reference.

0
source

Source: https://habr.com/ru/post/1395169/


All Articles