-2

I am trying to call BOOL in +(void) method but I can't. Why it would not set in +(void) method? While it is working on all -(void) methods.

.h

@property (nonatomic, assign) BOOL line;

.m

+ (void)normalizeCell:(UITableViewCell *)cell withLables:(NSArray *)lbls sx:(CGFloat)sx widths:(NSArray *)widths
if (![cell.contentView viewWithTag:22])
{
    UIImageView *gb = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"table.png"]];
    gb = 22;
    [cell.contentView addSubview:gb];
    [gb release];
}
Luai Kalkatawi
  • 1,492
  • 5
  • 25
  • 51
  • Please stop and read this entire Q&A: http://stackoverflow.com/questions/1053592/what-is-the-difference-between-class-and-instance-methods – Aaron Brager Dec 30 '13 at 13:11

2 Answers2

3

You can't use your header property in class method (+), only in instance method (-).

This is some kind of static method known from other programming language. In objective-c you can use class method to do some operation which fit to class you've created, but you have to remember that using a class method is NOT an object operation. You don't have to creating objects to use class method and of course you can't access properties of the objects.

Jakub
  • 13,712
  • 17
  • 82
  • 139
  • Thanks. So there is not any solution for that? – Luai Kalkatawi Dec 30 '13 at 12:57
  • I've edit my answer with explanation. You didn't provide any information what's your problem - using class method vs instance method is more like proposal of solution of something, but you didn't mentioned what your first problem here. – Jakub Dec 30 '13 at 12:59
  • My problem that I can't call `line` boolean inside the void. Even when I type line I get nothing. I hope this help you – Luai Kalkatawi Dec 30 '13 at 13:01
  • So the solution is - if you want to use object property you should use instance method (-) NOT class method (+). That's it. Like I said (+) method can't have access to properties, because object doesn't exists. What's your trying to do here make no sense. – Jakub Dec 30 '13 at 13:15
1

+ methods are class level methods and properties are instance level variables. Therefor it's not possible to set them, on what instance would they be set? Class level methods should not be used if you need to keep state. You can keep state if you really want to like this.

+ (BOOL)myBool:(NSNumber *)boolValue{

    static BOOL myBool = false;
    if (boolValue){
        myBool = [boolValue boolValue];
    }
    return myBool;
}

If you want this to not be part of the public interface just put this directly in the .m file so it's invisible for other classes. Then when you are inside your other class method you can do.

BOOL b = [self myBool:nil]; // get value
[self myBool:[NSNumber numberWithBool:YES]]; // set value

If you for reason would like to access this from your instances you can like this.

BOOL b = [MyClass myBool:nil];
[MyClass myBool:[NSNumber numberWithBool:NO]];
Peter Segerblom
  • 2,773
  • 1
  • 19
  • 24