Best way to define private methods for a class in Objective-C

后端 未结 12 1955
小鲜肉
小鲜肉 2020-11-22 14:34

I just started programming Objective-C and, having a background in Java, wonder how people writing Objective-C programs deal with private methods.

I understand there

12条回答
  •  盖世英雄少女心
    2020-11-22 15:10

    You could use blocks?

    @implementation MyClass
    
    id (^createTheObject)() = ^(){ return [[NSObject alloc] init];};
    
    NSInteger (^addEm)(NSInteger, NSInteger) =
    ^(NSInteger a, NSInteger b)
    {
        return a + b;
    };
    
    //public methods, etc.
    
    - (NSObject) thePublicOne
    {
        return createTheObject();
    }
    
    @end
    

    I'm aware this is an old question, but it's one of the first I found when I was looking for an answer to this very question. I haven't seen this solution discussed anywhere else, so let me know if there's something foolish about doing this.

提交回复
热议问题