Objective-C (aClassName *)

后端 未结 3 984
迷失自我
迷失自我 2021-01-28 21:46

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,

相关标签:
3条回答
  • 2021-01-28 22:28

    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.

    0 讨论(0)
  • 2021-01-28 22:34

    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

    0 讨论(0)
  • 2021-01-28 22:39

    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.

    0 讨论(0)
提交回复
热议问题