在Objective-C中声明静态成员变量,如Java

时间:2022-09-07 20:04:07

How can I make an Objective-C class with class-level variables like this Java class?

如何创建一个具有类级变量的Objective-C类,比如这个Java类?

public class test
{

    public static final String tableName = "asdfas";    
    public static final String id_Column = "_id";
    public static final String Z_ENT_Column = "Z_ENT";

}

I want to access them without making an instance, like:

我想要访问它们而不需要做一个实例,比如:

String abc = test.tableName;

6 个解决方案

#1


29  

It looks like you want to create constants (since you are using final in your question). In Objective-C, you can use extern for that.

看起来您想要创建常量(因为您在问题中使用的是final)。在Objective-C中,你可以使用extern。

Do something like this:

这样做:

1) Create a new Objective-C class named Constants.

1)创建一个新的名为constant的Objective-C类。

2) In the header (.h) file:

2)在header (.h)文件中:

extern const NSString *SERVICE_URL;

3) In the implementation (.m) file:

3)在实现(.m)文件中:

NSString *SERVICE_URL = @"http://something/services";

4) Add #import "Constants.h" to any class where you want to use it

4)添加#导入“常量。h"到任何你想要使用它的类

5) Access directly as NSString *url = SERVICE_URL;

5)直接作为NSString *url = SERVICE_URL访问;


If you don't want to create constants and simply want to use static in Objective-C, unfortunately you can only use static in the implementation (.m) file. And they can be accessed directly without prefixing the Class Name.

如果您不想创建常量,只想在Objective-C中使用静态,那么不幸的是,您只能在实现(.m)文件中使用静态。可以直接访问它们,而无需在类名前加上前缀。

For example:

例如:

static NSString *url = @"something";

I hope this helps.

我希望这可以帮助。

#2


24  

Try it....

试试....

static NSString *CellIdentifier = @"reuseStaticIdentifier";

You can access direct value using synthesis property
or you can use NSUserDefaults for store and retrive value

您可以使用合成属性访问直接值,也可以使用NSUserDefaults来存储和检索值。

Description

描述

@interface MyClass : NSObject
+(NSString *)myFullName;
@end

Implementation :

实现:

#import "MyClass.h"

@implementation MyClass
static NSString *fullName = @"Hello World";

+(NSString *)myFullName{
  return fullName;
}
@end

Use:

使用:

#import "MyClass.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
  NSLog(@"%@",[MyClass myFullName]); //no instance but you are getting the value.
}

@end

Hope i helped.

希望我帮助。

#3


7  

You need to use a class method to access anything that can be called without making an instance.

您需要使用类方法来访问任何可以在不创建实例的情况下调用的内容。

@interface MyClass : NSObject
+(NSString *)myFullName;
@end

Implementation :

实现:

#import "MyClass.h"

@implementation MyClass
   static NSString *fullName=@"anoop vaidya";

+(NSString *)myFullName;{
    return fullName;
}
@end

How to use:

如何使用:

#import "MyClass.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
    NSLog(@"%@",[MyClass myFullName]); //no instance but you are getting the value.
}

@end

#4


1  

Objective-C doesn't have class variables

Objective-C没有类变量

I would recommend putting the static NSString in the implementation file of your class, and provide class methods to access it

我建议将静态NSString放到类的实现文件中,并提供类方法来访问它。

@implementation MyClass

static  NSString* str;

#5


1  

Could be done like that:

可以这样做:

@interface Test
 {
   static NSString *tableName;
 }

+(NSString *) getTableName;
@end

@implementation Test
+ (NSString *)getTableName
 {
    return tableName;
 }
@end

And then you fetch it:

然后你把它拿来

NSString *name = [Test getTableName];

#6


1  

I think the best way and more used is to use enums such as

我认为最好的方法和更多的使用是使用enums例如

enum{
    GSPAYMENT_METHOD_CARD = 1,
    GSPAYMENT_METHOD_CASH = 2,
    GSPAYMENT_METHOD_VOID = 3
};
typedef NSUInteger PaymentMethodType;

Just before the @interface GSPaymentMethod

就在@interface GSPaymentMethod之前。

This way you can use those constants anywhere by just including the .h file

这样,只要包含.h文件,就可以在任何地方使用这些常量

For example

例如

[self newPayment:GSPAYMENT_METHOD_CASH]

(自我newPayment GSPAYMENT_METHOD_CASH):

#1


29  

It looks like you want to create constants (since you are using final in your question). In Objective-C, you can use extern for that.

看起来您想要创建常量(因为您在问题中使用的是final)。在Objective-C中,你可以使用extern。

Do something like this:

这样做:

1) Create a new Objective-C class named Constants.

1)创建一个新的名为constant的Objective-C类。

2) In the header (.h) file:

2)在header (.h)文件中:

extern const NSString *SERVICE_URL;

3) In the implementation (.m) file:

3)在实现(.m)文件中:

NSString *SERVICE_URL = @"http://something/services";

4) Add #import "Constants.h" to any class where you want to use it

4)添加#导入“常量。h"到任何你想要使用它的类

5) Access directly as NSString *url = SERVICE_URL;

5)直接作为NSString *url = SERVICE_URL访问;


If you don't want to create constants and simply want to use static in Objective-C, unfortunately you can only use static in the implementation (.m) file. And they can be accessed directly without prefixing the Class Name.

如果您不想创建常量,只想在Objective-C中使用静态,那么不幸的是,您只能在实现(.m)文件中使用静态。可以直接访问它们,而无需在类名前加上前缀。

For example:

例如:

static NSString *url = @"something";

I hope this helps.

我希望这可以帮助。

#2


24  

Try it....

试试....

static NSString *CellIdentifier = @"reuseStaticIdentifier";

You can access direct value using synthesis property
or you can use NSUserDefaults for store and retrive value

您可以使用合成属性访问直接值,也可以使用NSUserDefaults来存储和检索值。

Description

描述

@interface MyClass : NSObject
+(NSString *)myFullName;
@end

Implementation :

实现:

#import "MyClass.h"

@implementation MyClass
static NSString *fullName = @"Hello World";

+(NSString *)myFullName{
  return fullName;
}
@end

Use:

使用:

#import "MyClass.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
  NSLog(@"%@",[MyClass myFullName]); //no instance but you are getting the value.
}

@end

Hope i helped.

希望我帮助。

#3


7  

You need to use a class method to access anything that can be called without making an instance.

您需要使用类方法来访问任何可以在不创建实例的情况下调用的内容。

@interface MyClass : NSObject
+(NSString *)myFullName;
@end

Implementation :

实现:

#import "MyClass.h"

@implementation MyClass
   static NSString *fullName=@"anoop vaidya";

+(NSString *)myFullName;{
    return fullName;
}
@end

How to use:

如何使用:

#import "MyClass.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
    NSLog(@"%@",[MyClass myFullName]); //no instance but you are getting the value.
}

@end

#4


1  

Objective-C doesn't have class variables

Objective-C没有类变量

I would recommend putting the static NSString in the implementation file of your class, and provide class methods to access it

我建议将静态NSString放到类的实现文件中,并提供类方法来访问它。

@implementation MyClass

static  NSString* str;

#5


1  

Could be done like that:

可以这样做:

@interface Test
 {
   static NSString *tableName;
 }

+(NSString *) getTableName;
@end

@implementation Test
+ (NSString *)getTableName
 {
    return tableName;
 }
@end

And then you fetch it:

然后你把它拿来

NSString *name = [Test getTableName];

#6


1  

I think the best way and more used is to use enums such as

我认为最好的方法和更多的使用是使用enums例如

enum{
    GSPAYMENT_METHOD_CARD = 1,
    GSPAYMENT_METHOD_CASH = 2,
    GSPAYMENT_METHOD_VOID = 3
};
typedef NSUInteger PaymentMethodType;

Just before the @interface GSPaymentMethod

就在@interface GSPaymentMethod之前。

This way you can use those constants anywhere by just including the .h file

这样,只要包含.h文件,就可以在任何地方使用这些常量

For example

例如

[self newPayment:GSPAYMENT_METHOD_CASH]

(自我newPayment GSPAYMENT_METHOD_CASH):