Objective-C (aClassName *)

2020-05-05 07:52发布

I'm a kind of self-programmer-made-man, so I'm missing some basic knowledge from time to time.

That's why I'm unable to really define well the topic of my question, because I don't know how to name it and how it works (but I know answer will seem trivial to many of you). When you know what's you're looking for it's a lot easier to find answers.

In objective-c, I can see lines of code like this :

aClassName *myObject = (aClassName *)[NSEntityDescription insertNewObjectForEntityForName:@"aClassName" inManagedObjectContext:app.managedObjectContext];

What's bother me is that (aClass *) part. What's this ?

I know/feel it's related to very basic knowledge but I can't name it so I can't find it.

My guess (and that's how I use it up to now) is that's used for calling class methods (+) but i'm not sure of it and it may be more deep that what I understand.

Thanks for any explanation.

标签: objective-c
3条回答
乱世女痞
2楼-- · 2020-05-05 07:59

thats simply mean casting the object to type of the class inside the brackets for example here we cast the defention UITableViewCell to CustomCell

    CustomeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = (CustomeCell*)[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

... class methode is a different somthing its function that you can call without need to define an instance of the class ... when you call a instance method you need to do like this

Class *obj = [Class alloc] init];
[obj funtionName];

in Class ethod you just do like this

[Class funtionName];

hope this will be helpful.

查看更多
孤傲高冷的网名
3楼-- · 2020-05-05 08:00

It is type cast:

double x = 5.0;
int y = (int)x;

I don't know why it is there in your case, as far as I know that method returns id, so the cast is not necessary (even without it no compiler warning will be generated).

Regarding “where to find such information”: Objective-C/C++ are built upon C and C++ correspondingly, so I'd recommend to learn basics of those languages first.

查看更多
萌系小妹纸
4楼-- · 2020-05-05 08:08

It's a cast, in this case a down cast (even because up casts are implicit).

A cast is an operation that the developer does while writing the code to hint the compiler that a type is narrower than the one the compiler is thinking about.

Think about the following situation:

Class *a = [[Subclass alloc] init];
Subclass *b = a;

(assume that Subclass is a subclass of Class)

This won't compile because a is statically defined with a type which is not contained in Subclass. But the assignment wouldn't create any problem dynamically because a is used to store a Subclass object in practice.

So what you do? You place a cast Subclass *b = (Subclass*)a; to tell the compiler "trust me and ignore typechecking that assignment, I assert that it will be valid a runtime", and you automagically remove the compilation error. Forcing this behaviour of course removes type safety from your code, so you must know what you are doing.

Of course to understand the meaning of a cast in this situation you must at least know about inheritance and objects..

In your specific situation the return type of the method +(id)insertNewObjectForEntityForName:... is id, which is the least specific kind of object in ObjectiveC, it always needs to be casted to something if not stored just like a plain id

查看更多
登录 后发表回答