About pointers in Objective-C
Asked Answered
T

1

5

I stumbled across the following and can't seem to work out why this works. Please can you explain why I don't need to use a pointer before range?

NSString *d = @"The quick brown fox";
NSRange range = [d rangeOfString:@"brown"];
Tanto answered 30/11, 2011 at 23:18 Comment(0)
E
9

NSString is an object type. All object types are pointers and can't be created on the stack. NSRange is a C-struct. Structs can be created on the stack, and thus aren't necessarily all pointers.

There isn't a good guide to know which ones are objects, and which are structs. You'll just have to check for each type as you move forward.

Erratum answered 30/11, 2011 at 23:20 Comment(3)
+1 To the OP: remember that Objective-C is a super-set of C, adding OOP features. But even if you can code in a OOP way, you still have to know (at least) a few C basics.Charr
A solid understanding of heap vs stack allocations would be very handy here.Erratum
Not even a solid one... Just a few basics would actually help, IMHO : )Charr

© 2022 - 2024 — McMap. All rights reserved.