Difference between [object variable] and object.variable in Obj-C?

I worked on the program today and hit this strange mistake. I had a UIButton with an assigned action. The action was something like this:

-(void) someaction:(id) e
{
    if ([e tag]==SOMETAG)
    {
        //dostuff
    }
}

What bothers me is that when I first wrote it, the if line was

if (e.tag==SOMETAG)

Xcode refused to compile it, saying

error: request for member 'tag' in 'e', which is of non-class type 'objc_object*'

but I thought these two equivalents.

So, under what circumstances do they not coincide?

+3
source share
5 answers

, , , . "" , Key-Value-Coding, .

, someObject.variable [someObject variable].

id, - . id - , void *.

, , .

((MyObject*)e).tag
+5

, . , , , , , . @property. . : Objective-C, :

#import <Cocoa/Cocoa.h>
@interface AClass
{
}

-(NSString*) aProperty;

@end

@implementation AClass

-(NSString*) aProperty
{
    return @"some text";
}

@end

int main()
{
    AClass* foo = [[ACLass alloc] init];
    NSLog(@"%@", foo.aProperty);
    return 0;
}

, , , , (.. id).

+2

getj.prop getter (handwavily), [obj prop] setter, [obj setProp: blah]. , , NSNumberFormatter, . , .

((Foo *) obj).prop, .

, id ( id int), UIView ( UIBarButtonItem). if ([(id<NSObject>)e isKindOfClass:[UIView class]] && [(UIView*)e tag] == sometag), , e <NSObject> .

+1

id. , .

0

, , : -)

You do not directly access the instance variable, point notation is only used to obtain properties using the getter and setter methods that you @synthesized. You send a message to the object, which in this case calls the getter / setter methods. You cannot use dot notation to directly access a member variable or call a method directly — you must configure it as a property.

0
source

Source: https://habr.com/ru/post/1749498/


All Articles