Why is there NSInteger and not NSLong or even NSLongLong? [duplicate] - objective-c

This question already has answers here:
When to use NSInteger vs. int
(8 answers)
Closed 6 years ago.
NSInteger is integer object. So surely there should be long object?

NSInteger and NSUInteger are platform specific, so on 32 bit system is declared as an int and on 64 bit system it is declared as a long.

NSInteger is a foundation type, as you can see here.
There are also other useful primitive types, like NSRect, NSSize, etc.
Apple simply chose not to have a NSLong or NSLongLong type defined in foundation.
More information as to when to use NSInteger (and what the considerations are behind them) can be found in this related question.

Related

Why would we use a pointer in an NSInteger but not in an NSNumber? [duplicate]

This question already has answers here:
What's the difference between NSNumber and NSInteger?
(5 answers)
Closed 6 years ago.
I have an NSInteger that doesn't use a pointer and an NSNumber that does use a pointer. Can someone explain to me why this is the case? All my teacher said was the NSInteger is being used as a type alias but I'm not familiar with that either yet.
This question asked a direct question asking for the reason there was no pointer in NSInteger; not asking for all the differences.
Bear in mind that Objective-C is C. Thus:
An NSInteger is a scalar, a built-in C data type (an integer). [The actual size of this integer depends on the architecture, 32-bit vs. 64-bit. But it is still some form of C integer.]
An NSNumber is an object; Objective-C object references are represented as C pointers.

How can I get sizeof class based on NSObject [duplicate]

This question already has answers here:
Checking the size of an object in Objective-C
(6 answers)
Closed 8 years ago.
I'm new to obj-c . During these day's practices I noticed that every class based on NSObject can't have an entity like : NSObject en; in c++ but NSObject* en instead.
But, sometimes I need to know the Size of an Object.I can't simply write sizeof(en) because en is a pointer var.I can't simply use sizeof(NSObject) neither for the compiler telling me Application of sizeof to interface 'XXXX' is not supported on this architecture and platform.
I want to know if there is a way to get sizeof(NSObject) .If not,what the syntax is designed this for & any other ways to get the size.
From doc
class_getInstanceSize
Returns the size of instances of a class.
size_t class_getInstanceSize(Class cls)
Parameters cls A class object.
Return Value The size in bytes of instances of the class cls, or 0 if
cls is Nil.
But I doubt this is what you really want. Because I never found it useful and can't think a case it may be useful. (other than learning memory layout of objects and low level implementation details)
First, you should import malloc.h
If you use Non-ARC:
malloc_size(myObject);
if you are using ARC:
malloc_size((__bridge const void *) myObject));
This linker is a question similar to yours.

difference beetween string.length and [string length] in objective C [duplicate]

This question already has answers here:
Dot notation vs. message notation for declared properties [closed]
(19 answers)
Closed 9 years ago.
Can someone explain to me why in Objective C
when declaring a string with
NSString *string;
I can use both string.length and [string length] to return the length of the string?
in the .h there is only the method
-(NSUInteger *)length
So why can I use the (dot) notation?
It's just syntactic sugar, they are both the same. Dot notation came in with #property but behind the scenes it's converted into method calls for you. Indeed, any #property Definition you do have will generate associated accessor methods and they are what is actually called. Again, you can call the method names rather than using dot notation.
Try to use the notation which makes the most sense, both to you and for the context. Dot notation can't be used with methods that take any parameters, but also only use it for methods without side effects.
Interesting article on the topic at the big nerd ranch.

What's the equivalent of Math.abs() in Objective-C? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert to absolute value in Objective-C
As i am playing with 2d graphics, i'd like to calculate amount of points object has moved between 2 CGPoints. Given that object can move in both direction, i am only interested in sheer number of points representing the difference.
In Java i'd Math.abs(startpoint.x - endpoint.x)
How can i do the same in Objective-C?
There are C functions from <math.h> that will do what you want:
abs(int val);
labs(long val);
llabs(long long val);
fabs(double val);
fabsf(float val);
fabsl(long double val):
Given that CGPoint structures are composed of CGFloats, you should use fabsf here.
Check out the Wikipedia page

Objective C: NSString* myVar; vs. NSString *myVar; [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Placement of the asterisk in Objective-C
I am confused, is there a difference between declaring a pointer as NSString* myVar; and NSString *myVar; ?
i.e. is the location of the asterix significant?
No, there is no difference. However I think that 2nd one is more readable.
int* a, b;
int *a, b;
In the 2nd one it is clear that a is a pointer but b is not. But in the 1st line it looks like both a and b are pointers which is not true.
But still, this is a personal choice and there is NO difference from compiler's point of view in the two lines.
No.