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.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to ask that what is that id? I don't understand what this id is. I got this code from a book and it says a generic type that's used to refer to any kind of object. Can anyone help me with this? I read it few times. Still can't get it.
void drawShapes (id shapes[], int count){
for (int i = 0; i < count; i++) {
id shape = shapes[i];
[shape draw];
}
} // drawShapes
id is an alias for an unknown Objective-C object. It can be used to declare any Objective-C object value.
In the example you have it is using an id rather than a specific class so that the code is not dependent on the class of shape.
Strictly speaking id is defined as a pointer to an objc_object struct.
typedef struct objc_object {
Class isa;
} *id;
In practical terms this means any Objective-C object.
However don't confuse this with NSObject *. While in many cases the equivalence may hold, there are classes which do not descend from NSObject but are still valid Objective-C objects (and therefore whose type can be id). One notable example is NSProxy.
In the code you posted, the id stands for the type of items that will be stored in a C static Array. In particular, the id type indicates any Objective-C object.
Anyway, I would not recommend to use C static arrays in Objective-C to contains objects of unknown type, when you can achieve the same result by using an instance of NSArray.
id means "a reference to some random Objective-C object of unknown class" an example is when you make an at property for a uibutton sometimes it will come up as id but setting it as uibutton will help xcode fill in blanks for you while your typing because now xcode knows exactly what this object is. in your situation shape could be a string or a number or something else if you were just looking at that line of code though passing anything into it could give another line a error later on.
This question already has answers here:
Would it be beneficial to begin using instancetype instead of id?
(5 answers)
Closed 9 years ago.
Recent Objective-C compilers introduce the 'instancetype' keyword, which among other things can be used to provide typed collections. . .
I saw another purpose of instancetype, which was using it in 'objectWith' type methods on classes. For example:
#interface Car
+(instancetype)carWithWheels:(NSArray*)wheels;
#end
The justification was that the compiler will do type checking for initWith methods, but not for 'objectWith' methods.
Besides being potentially easier to type, what is the benefit of using 'instancetype' in place of the actual class-name? Eg:
#interface Car
+(Car*)carWithWheels:(NSArray*)wheels;
#end
By using instancetype, you're saying that subclasses will return an object of the subclass.
If we have
#interface Car
+(instancetype)carWithWheels1:(NSArray *)wheels;
+(Car *)carWithWheels2:(NSArray *)wheels;
#end
and
#interface VolkswagenBeetle : Car
#end
then +[VolkswagenBeetle carWithWheels1:] is guaranteed to return an instance of VolkswagenBeetle; but +[VolkswagenBeetle carWithWheels2:] might return a Buick, a Caddilac, or a ChittyChittyBangBang.
You can subclass Car and still benefit from instancetype. Of course you could achieve this using id, but you'd loose type checking which you gain from instancetype.
I simply use it everywhere it's supposed to return object of current class (subclasses return object of that subclass), specifically in convenience methods and stuff.
There is a great answer which could help.
This question already has answers here:
What does this ' ->' mean in c/objective-c?
(7 answers)
What is the difference between '->' (arrow operator) and '.' (dot operator) in Objective-C?
(3 answers)
Dot (".") operator and arrow ("->") operator use in C vs. Objective-C
(5 answers)
Closed 10 years ago.
I have been looking at some code and come across the symbol -> being used like obj->method(argument); I have done a little bit of research and found it basically is the same as [obj method:argument]; but I am unsure what -> actually is or does.
So my question is, what does the -> symbol mean in objective-c?
It means the same as the struct dereference operator does in C, which is used to access fields within the struct via a pointer:
struct mystruct
{
int field;
};
struct mystruct *mystruct = ...;
printf("field=%d\n", mystruct->field);
In Objective-C it can also be used to access fields within Objective-C objects:
#interface MyObj : NSObject
{
#public
int field;
}
#end
MyObj *myObj = [[MyObj alloc] init];
NSLog(#"field=%d", myObj->field);
Note that you can only access these fields externally if they are declared #public.
I have been looking at some code and come across the symbol -> being
used like obj->method(argument); I have done a little bit of research
and found it basically is the same as [obj method:argument]; but I am
unsure what -> actually is or does.
So my question is, what does the -> symbol mean in objective-c?
Exactly the same thing it means in C; it is for accessing an item in a C structure. Way back in the days of yore, Objective-C was implemented purely as a C preprocessor extension + a runtime. Classes were nothing more than concatenated C structures and the preprocessor turned each ivar access into self->ivar.
I.e. ivar and self->ivar do the same thing (in a method of class).
Now, you can use -> to poke at some other object's (#public) ivars. But don't. That breaks encapsulation exactly because Objective-C's line of encapsulation is drawn at the method interface. Always use the setters/getters such that behavior can be either observed or overridden.
Finally, no, there is nothing like obj->method(argument) anymore. There was, once, in a failed experiment called Modern Syntax, but it was abandoned because it was a pointless waste of time. You can't use -> to invoke methods.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to wrap a Struct into NSObject
Can the new Clang Objective-C literals be redirected to custom classes?
I have a custom struct:
typedef struct {
float f1;
float f2;
} MYCustomStruct;
that I need to add to an NSArray. I've already written a category to create NSValues of these structs, which I then add to the NSArray, however I'd like to simplify that even further using boxed expressions, if possible. I'd love to be able to do this:
#[#(instanceOfMYCustomStruct)];
however, I'm confronted with the following error:
Illegal type 'MYCustomStruct' used in a boxed expression
Is there a way to use boxed expressions with custom structs?
I would use a NSValue to box a struct, as it has built-in support for it. Unfortunately, you cannot use objective-c's cool literals for it, though:
struct foo myStruct;
NSValue *val = [NSValue valueWithBytes:&myStruct objCType:#encode(typeof(myStruct))];
// to pull it out...
struct foo myStruct;
[val getValue:&myStruct];
While this may be unwieldy & ugly amidst other objc code, you have to ask yourself - why are you using a struct in the first place in Objective-C? There are few speed performances gained over using an object with a few #property(s), the only real reason I could see is if you are integrating with a C library for compatibility with memory layouts, and even then, the structure of an objective-c object's memory layout is well-defined, so long as the superclass doesn't change.
So what is your real purpose in boxing a struct? If we have that, we can help you further.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Constants in Objective C
I'm designing a controller and I'm gonna need some constants inside it (locally, just for that controller). Looking at some sample code provided by Apple, I can see these lines:
#import "Constants.h"
#define kTextFieldWidth 260.0
static NSString *kSectionTitleKey = #"sectionTitleKey";
static NSString *kSourceKey = #"sourceKey";
static NSString *kViewKey = #"viewKey";
const NSInteger kViewTag = 1;
Can anyone explain to me what the difference between them is? Which style should I use? Are they dependent on the type of object/value you assign to them? Meaning use: static NSString * for strings, #define for floats and NSInteger for integers? How do you make the choice?
The #define keyword is a compile time directive that causes the define'd value to be directly injected into your code. It is global across the entire program and all linked libraries. So you can strike that off the list, based on your desire to create a constant for the controller only.
The main difference between static and const is that static variables can be changed after initialization, const ones cannot. If you want to be able to modify your variable after initialization then you should use the static keyword.
Hope that helps.
As Scott and benzado pointed out that is the best way to define your constant values. However as far as defines go it is harder to debug using defines as you can usually not easily see the expanded value in a debugger. You will only need to add an extern declaration to the header file of your class if your intentions are to expose the variable globally. And the next thing to remember is to put the const declaration after the pointer (*) or else you will get warnings of discard qualifiers from pointer in most uses.