Why don't we use * while we create the object of NSRange - iphone

Can anybody tell me that Why don't we use * with NSRange object.Whenever i use * with NSRange it gives error why it's giving error?

Because NSRange is a struct, not a class. Structs are value types, and therefore you do not use pointers with them like you do with class instances (* is the pointer dereferencing operator).

This is the structure of NSRange :
typedef struct _NSRange {
NSUInteger location;
NSUInteger length;
} NSRange;
Now tell me do you think we required any pointer ? why we required the pointer ? If you can give the answer than that is your solution.

Related

is there any way to get the return value from a method by using #selector except using double pointer?

I don't want to use double pointer. I am using a function in simpler form as below.
-(NSString *) getName
{
return name;
}
So what is the correct way to take the returned NSString *?
By using #selector(getName) i am not able to get the returned value name.
Thank you in advance
You should use NSInvocation object instance for calling a selector and resolving returned result.
performSelector: does give you the return value directly.
NSString * s = #"NEXT WE HAVE NUMBER FOUR, 'CRUNCHY FROG'.";
NSString * l = [s performSelector:#selector(lowercaseString)];
NSLog(#"%#", l); // prints "next we have number four, 'crunchy frog'."

Return type from valueForKeyPath:?

This is probably pilot error on my part, but I am a little confused why this does not return an int (as thats the type of the property identified by the key path). Does valueForKeyPath: return an object instead, can anyone explain.
// Simple Object
#interface Hopper : NSObject
#property(nonatomic, assign) int mass;
#end
// Test
Hopper *hopper = [[Hopper alloc] init];
[hopper setMass:67];
NSLog(#"HOPPER: %d", [hopper valueForKeyPath:#"mass"]);
.
WARNING: Conversion specifies type 'int' but the argument has type 'id'
Yes, it returns an objc object:
- (id)valueForKeyPath:(NSString *)keyPath;
Details for automatic conversions from non-objc objects to objc objects (e.g. NSNumber and NSValue) is covered in Accessor Search Patterns for Simple Attributes.
Therefore, you would use the objc object format specifier %#:
NSLog(#"HOPPER: %#", [hopper valueForKeyPath:#"mass"]);
valueForKeyPath returns an object. int and char types are not objects. Access the property via the . operator or similar.
NSLog(#"HOPPER: %d", [hopper mass]);
NSLog(#"HOPPER: %d", hopper.mass);
Edit: Didn't fully read example code, updated answer

XCODE incompatible pointer type error

I am getting a casting error. My app is reading a text file from a webpage using 'stringWithContentsOfURL' method. I want to parse the individual lines into separate components. This is a snippet of the code.
int parameterFive_1 = 0;
parameterFive_1_range = NSMakeRange(0,10)
lines = [response componentsSeparatedByString:#"\r"];
parameterFive_1 = CFStringGetIntValue([[lines objectAtIndex:i] substringWithRange:parameterFive_1_range]);
I am getting the following error message:
" Implicit conversion of an Objective-C pointer to 'CFStringRef' (aka 'const struct __CFString *') is disallowed with ARC"
I thought it might be the compiler option but changing it to the default is not making a difference. Can anyone provide any insight?
Just cast the NSString* to CFStringRef to satisfy ARC:
parameterFive_1 = CFStringGetIntValue((__bridge CFStringRef)[[lines objectAtIndex:i] substringWithRange:parameterFive_1_range]);
The __bridge keyword here lets ARC know that it doesn't need to transfer ownership of the string.

how to pass pointer and got the result in function?

please see code as follow :
- (BOOL)postAction :( NSString*) url params:(NSDictionary*) params bodySize:(NSNumber**)bodySize
{
...
int size = 1999;
NSNumber* value =[[NSNumber alloc] initWithInt:size];
bodySize = &value;
...}
use the function as follows:
NSNumber* size ;
[self postAction:#"http://webserver/ss.php" params:params bodySize:&size];
// can not got the size value at all…
int i = [size intValue];
//will throw nil exception !
my question is that how to correct this code above ?
many thanks for your help !
Regards
If you are using pass-by-reference in iOS or Mac OS X, you are probably doing it wrong.
Seriously-- pass by reference is an exceedingly rare pattern to use. It is pretty much entirely reserved to (NSError**) across the APIs.
Not to say that there isn't a reason to use pass-by-reference, but this isn't it.
Specifically, if you need to return an NSNumber, then return it!
- (NSNumber *) foo;
If that method returns nil, that is just as good as returning a BOOL NO. And it sets you up to follow the very common pattern of using NSError:
- (NSNumber *) foo: (NSError **) error;
Ideally, you should always check the pointer before dereferencing it:
If ( bodySize )
*bodySize = value;
I think you want this:
//bodySize = &value;
*bodySize = value;
I agree with the above two answers. To be more clear, NSNumber is an object (Unlike NSInteger or NSUInteger). So you should directly point your pointer to that object.

Why is there no * in this method declaration?

Here is the method declaration midway in Apple's documentation:
Learning Objective-C: A Primer
- (void)insertObject:(id) anObject atIndex:(NSUInteger) index
Why is there no * right after NSUInteger. I thought all objects were pointer types and all strongly typed pointers had to have a * character after it.
NSUInteger is not an object type, it is a typedef to unsigned int.
The only reason that you would actually want to use a * in this context would be if you wanted to get the address of an int and store something in it. (Some libraries do this with error messaging). An example of this:
-(void) methodName: (NSUInteger *) anInt {
*anInt = 5;
}
NSUInteger a;
[obj methodName: &a]; //a is now 5