Objective C static class variables

后端 未结 4 814
悲哀的现实
悲哀的现实 2020-12-12 17:14

I am new to Objective C and am reading a book called \"Visual Quickstart Guide: Objective-C\" by Steven Holzner, Peachpit Press

In Chapter 6: Object Oriented Program

4条回答
  •  悲哀的现实
    2020-12-12 17:58

    You declare the static variable in the implementation file (.m file). This should work:

    // TheClass.h
    @interface TheClass : NSObject
    + (int)count;
    @end
    
    // TheClass.m
    static int theCount = 0;
    
    @implementation TheClass
    + (int) count { return theCount; }
    @end
    

    It's not a class variable per se; Objective-C has no notion of a class variable. However, coupled with the class method for retrieving this variable, it functions similarly to a class variable. However, it's really just a C static variable that's accessible by the class's implementation.

提交回复
热议问题