Objective-C ARC pointer ownership vs C++
Lets say I have a high level class that instantiates an object and then
passes it down to a lower level class:
- (void) doSomeStuff {
MyBusinessObject* obj = [[MyBusinessObject alloc] init];
[obj setFoo:@"bar"];
[dataManager takeObj:obj withKey:@"abc" andKey:@"def"];
}
and then in the implementation of takeObj I want to keep two different
dictionaries...
- (void) takeObj:(MyBusinessObject*)obj withKey:(NSString*)key1
andKey:(NSString*)key2 {
[primaryDict setObject:obj forKey:key1];
[secondaryDict setObject:obj forKey:key2];
}
Now, what I want is for the ownership of obj to be passed down to my data
manager and have primaryDict hold strong references and secondaryDict hold
weak references. This is how I would do it in C++:
map<string, unique_ptr<MyBusinessObject>> primaryDict;
map<string, MyBusinessObject*> secondaryDict;
The takeObj function would accept a unique_ptr<MyBusinessObject> that
would be passed down with std::move. That would then be moved again into
primaryDict and a weak reference would be added with a raw pointer in
secondaryDict.
My question is--what is the correct way to tell the Objective-C ARC system
to manage my references in that way?
No comments:
Post a Comment