开发者

Generic return type for primitives

开发者 https://www.devze.com 2023-02-15 23:20 出处:网络
Is there a return type for \"any primitive\" similar to the way you can use NSObject as the retur开发者_StackOverflown type for any object? I tried using id, but the compiler was giving me an error th

Is there a return type for "any primitive" similar to the way you can use NSObject as the retur开发者_StackOverflown type for any object? I tried using id, but the compiler was giving me an error that it was trying to convert a primitive to a reference.

Here's what I'm trying to do. :

-(void)setObject:(NSObject*)obj forKey:(NSString*)key {
    [sharedInstance.variables setObject:obj forKey:key];
}


-(NSObject*)getObjectForKey:(NSString*)key {
    return [sharedInstance.variables objectForKey:key];
}


-(void)setValue:(???)value forKey:(NSString*)key {
    [sharedInstance.variables setValue:value forKey:key];
}


-(???)getValueForKey:(NSString*)key {
    return [sharedInstance.variables valueForKey:key];
}

The alternative that I have though of is to use separate methods (getIntForKey, getFloatForKey, etc.) to access the values.


1) Read Key-Value Coding Article in XCode documentation - all answers are there

2) There's an object NSValue, which resembles your "NSObject". NSValue can store plain-old-data inside itself.

PS

"Scalar and Structure Support Key-value coding provides support for scalar values and data structures by automatically wrapping, and unwrapping, of NSNumber and NSValue instance values. Representing Data as Objects The default implementations of valueForKey: and setValue:forKey: provide support for automatic object wrapping of the non-object data types, both scalars and structs. Once valueForKey: has determined the specific accessor method or instance variable that is used to supply the value for the specified key, it examines the return type or the data type. If the value to be returned is not an object, an NSNumber or NSValue object is created for that value and returned in its place. Similarly, setValue:forKey: determines the data type required by the appropriate accessor or instance variable for the specified key. If the data type is not an object, then the value is extracted from the passed object using the appropriate -Value method."


I would have thought id is the perfect candidate here too... could this just be a casting issue you're seeing?

i.e. the id implicitly implies a pointer so in my mind I see id as an objective c equivalent to the c void*

In other words where you have a NSObject* you could replace this with id such as

-(NSObject*)myMethod1
{
}

so can be done for any returned primitive with

-(id)myMethod1
{
}

i.e. not an id*

Also I expect this was just a copy/paste thing but incase it also causes issues

-(void)setValue:(???)value forKey:(NSString*)key {
    [sharedInstance.variables setValue:num forKey:key];
}

should probably be

-(void)setValue:(???)value forKey:(NSString*)key {
    [sharedInstance.variables setValue:value forKey:key];
}


I eventually worked through this. The ultimate solution was to have separate accessor/mutator methods per type. So now I have setIntForKey, setBoolForKey, getIntForKey, getBoolForKey, etc. The drawback is quite obvious, in that I can't call one method to set values and another to retrieve them. The advantages are numerous, however. Because the compiler knows what object or primitive type the method is expecting at compile time, I gain compile time checking for all of these methods. Additionally, I don't have to worry with casting the retrieved values to their primitive types (obviously the returned NSObjects are a different story).

0

精彩评论

暂无评论...
验证码 换一张
取 消