Why is there no * in this method declaration? - iphone

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

Related

How to pass an array to a method and then determine the array's size?

I have this method:
+ (NSData *) createWave: (short[])sampleData {
int i = [sampleData count]; // Warning: Invalid receiver type 'short int *'
}
Inside this method, I'm trying to determine how many elements are in the samples array that was passed in. But I'm getting the warning above (I get the same warning if I change samples to short *).
How can I pass an array like this, and then determine the array's size?
You can't.
Either make sure that the last element in your array is unique and check for that or pass in a size parameter as well i.e.
+ (NSData *) createWave:(short [])samples size:(size_t)count {
int i = count;
}
short[] isn't an object so you can't call methods on it - that's why you're getting a warning (and probably a crash if you run the code!)
You are trying to use a C style array as a parameter and then access it as an Objective-C object. (I am assuming sampleData and samples are supposed to be the same). Use an NSArray of NSNumbers instead because with C style arrays you need to know the length.
+ (NSData *) createWave: (NSArray*)sampleData {
int i = [sampleData count];
}

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 don't we use * while we create the object of NSRange

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.

NSComparisonResult and NSComparator - what are they?

What is NSComparisonResult and NSComparator?
I've seen one of the type definitions, something like that:
typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);
Is it any different from a function pointer?
Also, I can't even guess what the ^ symbol means.
^ signifies a block type, similar in concept to a function pointer.
typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);
// ^ ^ ^
// return type of block type name arguments
This means that the type NSComparator is a block that takes in two objects of type id called obj1 and obj2, and returns an NSComparisonResult.
Specifically NSComparator is defined in the Foundation Data Types reference.
And to learn more about C blocks, check out this ADC article Blocks Programming Topics.
Example:
NSComparator compareStuff = ^(id obj1, id obj2) {
return NSOrderedSame;
};
NSComparisonResult compResult = compareStuff(someObject, someOtherObject);
Jacob's answer is good, however to answer the part about "how is this different than a function pointer?":
1) A block is not a function pointer. Blocks are Apple's take on how to make functions first class citizens in C/C++/Objective-C. It's new to iOS 4.0.
2) Why introduce this strange concept? Turns out first class functions are useful in quite a few scenarios, for example managing chunks of work that can be executed in parallel, as in Grand Central Dispatch. Beyond GCD, the theory is important enough that there are entire software systems based around it. Lisp was one of the first.
3) You will see this concept in many other languages, but by different names. For example Microsoft .Net has lambdas and delegates (no relation to Objective-C delegates), while the most generic names are probably anonymous functions or first class functions.
NSComparisonResult comparisionresult;
NSString * alphabet1;
NSString * alphabet2;
// Case 1
alphabet1 = #"a";
alphabet2 = #"A";
comparisionresult = [alphabet1 caseInsensitiveCompare:alphabet2];
if (comparisionresult == NSOrderedSame)
NSLog(#"a and a are same. And the NSComparisionResult Value is %ld \n\n", comparisionresult);
//Result: a and a are same. And the NSComparisionResult Value is 0
// Case 2
alphabet1 = #"a";
alphabet2 = #"B";
comparisionresult = [alphabet1 caseInsensitiveCompare:alphabet2];
if (comparisionresult == NSOrderedAscending)
NSLog(#"a is greater than b. And the NSComparisionResult Value is %ld \n\n", comparisionresult);
//Result: a is greater than b. And the NSComparisionResult Value is -1
// Case 3
alphabet1 = #"B";
alphabet2 = #"a";
comparisionresult = [alphabet1 caseInsensitiveCompare:alphabet2];
if (comparisionresult == NSOrderedDescending)
NSLog(#"b is less than a. And the NSComparisionResult Value is %ld", comparisionresult);
//Result: b is less than a. And the NSComparisionResult Value is 1

Convert unsigned char * to int * in Objective-C

I am trying to convert a unsigned char* to int * on in Objective-C on the iPhone. Is there any API that can help with the conversion?
Here's my best attempt:
-(BOOL)MyFunc:Version:(int *)nVer
{
unsigned char * uszVerSize;
//trying to assign string to int
nVer = uszVerSize[0] ;
}
Dear Lord, I think you have bigger problems than the one stated above.
You need to convert the chars to an int and return that.
return [[NSNumber numberWithUnsignedChar:uszVerSize] intValue];
You should also learn about pointers and how ints and chars differ in memory. You can't just assign a pointer to a char to a pointer to an int.
const char *asciiCString = [#"abc-zABC-Z" cString];
int cStringLen = [#"abc-zABC-Z" length];
for(i=0;i<cStringLen;i++) {
[asciiMArray addObject: [[NSNumber alloc] initWithInteger: asciiCString[i]]];
printf("%d\n",asciiCString[i]);
}
for(i=0;i<cStringLen;i++) {
NSLog(#"%#",[asciiMArray objectAtIndex: i]);
printf("%d\n",asciiCString[i]);
}
This is a code i wrote yesterday, to test some code of my learning face
It may look naive...but if it helps you....
asciiCString[i] returns you ASCII value of the char referenced at the index..
asciiMArray is a NSMutableArray Object