Reputation: 85
My Objective-C experience is not good, I would love if someone can help with this.
There are some other code around but I believe I simplified this to just the problem. What I am trying to do is to save a double (packed to NSNumber) with a key, below is my code:
.h
@interface MyClass : something
{
MyMap* usemap;
}
@end
@interface MyMap : something
@property (atomic, strong) NSMutableDictionary* mmap;
@end
.m
@implementation MyMap
@class MyClass;
NSMutableDictionary* mmap = [[NSMutableDictionary alloc] init];
@end
@implementation MyClass
MyMap* usemap = [MyMap alloc];
//-void { switch() { case x:
NSString* key = @"testkey";
NSNumber* value = [NSNumber numberWithDouble:1];
NSLog(@"Saving value: %@ to key: %@",value,key);
[usemap.mmap setObject:value forKey:key];
NSNumber* get = [usemap.mmap objectForKey:key];
NSLog(@"Saved value: %@ to key: %@",get,key);
Now this prints:
Saving value: 1 to key: testkey
Saved value: (null) to key: testkey
What am I missing? it should have saved the double number '1'.
Upvotes: 0
Views: 64
Reputation: 332
You need to override MyClass init
method and initialize MyMap in it usemap = [[MyMap alloc] init]
. Then override MyMap init method and initialize mmap in it self.mmap = [[NSMutableDictionary alloc] init]
.
Do rest of the work in MyClass overridden init
method. Here's a rough sketch:
.h
@interface MyClass : something
{
MyMap* usemap;
}
@end
@interface MyMap : something
@property (atomic, strong) NSMutableDictionary* mmap;
@end
.m
@implementation MyMap
@class MyClass;
- (instancetype)init {
if ((self = [super init]))
self.mmap = [[NSMutableDictionary alloc] init];
return self;
}
@end
@implementation MyClass
- (instancetype)init {
if ((self = [super init]))
{
usemap = [[MyMap alloc] init];
NSString* key = @"testkey";
NSNumber* value = [NSNumber numberWithDouble:1];
NSLog(@"Saving value: %@ to key: %@",value,key);
[usemap.mmap setObject:value forKey:key];
NSNumber* get = [usemap.mmap objectForKey:key];
NSLog(@"Saved value: %@ to key: %@",get,key);
}
return self;
}
@end
Hope it helps!
Upvotes: 1
Reputation: 42588
Setting up the internals of a object is done with an -init
method.
@implementation MyMap
@class MyClass;
// NO NO NO! don't even ask what this does
// NSMutableDictionary* mmap = [[NSMutableDictionary alloc] init];
// YES: Use -init to setup the initial state of an object.
- (instancetype)init {
self = [super init];
if (self != nil) {
// NOTE: _mmap is the instance variable for the property self.mmap.
_mmap = [[NSMutableDictionary alloc] init];
}
return self;
}
@end
Upvotes: 0
Reputation: 26876
Try this:
MyMap.h:
#import <UIKit/UIKit.h>
@interface MyMap : NSObject
@property (atomic, strong) NSMutableDictionary* mmap;
@end
MyMap.m:
#import "MyMap.h"
@interface MyMap ()
@end
@implementation MyMap
@end
ViewController.m:
MyMap *usemap = [[MyMap alloc] init];
usemap.mmap = [[NSMutableDictionary alloc] init];
NSString* key = @"testkey";
NSNumber* value = [NSNumber numberWithDouble:1];
NSLog(@"Saving value: %@ to key: %@",value,key);
[usemap.mmap setObject:value forKey:key];
NSNumber* get = [usemap.mmap objectForKey:key];
NSLog(@"Saved value: %@ to key: %@",get,key);
The result:
2017-01-21 20:48:59.563 TestOC[4161:50263] Saving value: 1 to key: testkey
2017-01-21 20:48:59.564 TestOC[4161:50263] Saved value: 1 to key: testkey
Upvotes: 0