0

Android:

public class DatabaseHandler extends SQLiteOpenHelper{

private interface Tables {
        final static String CONTACT_BOOK = "ContactBook";
}

//Access this Tables.CONTACT_BOOK to assign in some variables

}

I need to convert above code to iOS: I tried this:

// classA.m
@interface Tables : NSObject //Create @interface here
{
    NSString *CONTACT_BOOK;
}
@end

@implementation Tables
-(id)init
{
    CONTACT_BOOK = @"ContactBook";
    return self;
}
@end

@implementation classA

I didn't know this is right or wrong, can any one suggest me, how to do this in iOS.

Thanks in Advance

SampathKumar
  • 2,525
  • 8
  • 47
  • 82

1 Answers1

0

There are two kind of methods in Objective C.

  • Class method
  • Instance method

Class method can access directly using class name and instance method needs an object of class to access.

As per above Android code which can directly access class variable using it class method.

Actually in Objective C it's a bit different, you can not directly access property using its class name.

However you can achieve it using class methods.

@interface Test : NSObject

+ (NSString *) contactBook;

@end

@implementation Test

+ (NSString *) contactBook {

    static NSString *CONTACT_BOOK = nil;

    if (CONTACT_BOOK == nil) {
        CONTACT_BOOK = @"ContactBook";
    }
    return CONTACT_BOOK;
}

@end

Reference from this answer.

Community
  • 1
  • 1
Kampai
  • 22,848
  • 21
  • 95
  • 95