Objective-C, member variables and class variables

前端 未结 1 1224
不知归路
不知归路 2021-01-05 07:30

i need some help on understanding how to use class/member variable from a instance method in Objective-C.

Any snipplet / example is highly appreciated.

Thank

相关标签:
1条回答
  • 2021-01-05 07:53

    Objective-C doesn't have class variables, and what you call a member variable is called an instance variable. Instance variables can be referenced by name from within an instance method. And if you need the behavior of a class variable, you can use a file-level static instead.

    Here's a very quick sample:

    Foo.h

    @interface Foo : NSObject {
        NSString *foo;
    }
    @end
    

    Foo.m

    static NSString *bar;
    
    @implementation Foo
    - (void)foobar {
        foo = @"test"; // assigns the ivar
        bar = @"test2"; // assigns the static
    }
    @end
    
    0 讨论(0)
提交回复
热议问题