1

私は目的cに不慣れです。1 つのクラス ファイルにインターフェイスを追加し、そこでいくつかの変数を宣言しました。コンパイル中に、すべての宣言で「@interface および @protocol 内で変数を宣言できません」というエラーが表示されます。これはサンプルコードです

#import "State.h"

@interface State(private)
NSString *forgotPassword=nil;
NSMutableArray *CategorySelection = nil;
NSMutableArray *subCategorySelection= nil;
NSString *string = nil;
int Tag= 0;
int alertTag=0;
NSURL *stringURL =nil;
NSURL *videoURL =nil;
NSURL *imageURL = nil;
int loginCount = 0;
NSMutableArray *album;
NSString *videoFileName = nil;
int videoCounting=0;
int loginUserId = 0;
int ImageTagg = 0;

@end

@implementation State

+(void) setforgotPasswordText:(NSString *) passwordText{
    forgotPassword = passwordText;
}

私は初心者なので、この問題を解決するように案内してください。ありがとう。

4

4 に答える 4

4

クラスでカテゴリを宣言したようです。カテゴリにインスタンス変数を追加することはできません。

ただし、それらをクラス拡張に追加することはできます。そのために、カテゴリの名前 ( private) を削除し、初期化を削除して、コードを次のようにします。

@interface State () {
    // Note how the initialization is gone:
    NSString *forgotPassword;
    NSMutableArray *CategorySelection;
    // More variables; no initialization!
}
// More stuff...
@end

すべての初期化は、次のように、指定されたイニシャライザ(initメソッドなど) で行う必要があります。

-(id)init {
    if (self = [super init]) { // Yes, it's =, not ==
        // Perform all your initialization here:
        *forgotPassword= ...;
        *CategorySelection = ...;
    }
    return self;
}
于 2012-07-30T04:42:47.077 に答える
4

新しいクラス を宣言するだけの場合はState、インスタンス変数を次のように宣言します (中かっこ内で、明示的な初期化は行いません)。

@interface State
{
    NSString *forgotPassword;
    NSMutableArray *categorySelection;
    NSMutableArray *subCategorySelection;
    NSString *string;
    int tag;
    int alertTag;
    NSURL *stringURL;
    NSURL *videoURL;
    NSURL *imageURL;
    int loginCount;
    NSMutableArray *album;
    NSString *videoFileName;
    int videoCounting;
    int loginUserId;
    int imageTag;
}
@end

ARC を使用している場合は、これらすべてをゼロまたはゼロに設定するため、それらを初期化する必要はありません。ARC を使用していない場合 (なぜ使用しないのか)、メソッドでこれらを初期化しますinit

そして、あなたが独自の「セッター」を書いていることに気付きました(例setForgotPassword)。コンパイラにこれを行わせたい (つまり、それらを「合成」する) 場合は、まずこれらの変数をプロパティとして宣言します。次に例を示します。

@interface State

@property (nonatomic, strong) NSString *forgotPassword;
@property (nonatomic, strong) NSMutableArray *categorySelection;
@property (nonatomic, strong) NSMutableArray *subCategorySelection;
@property (nonatomic, strong) NSString *string;
@property int tag;
@property int alertTag;
@property (nonatomic, strong) NSURL *stringURL;
@property (nonatomic, strong) NSURL *videoURL;
@property (nonatomic, strong) NSURL *imageURL;
@property int loginCount;
@property (nonatomic, strong) NSMutableArray *album;
@property (nonatomic, strong) NSString *videoFileName;
@property int videoCounting;
@property int loginUserId;
@property int imageTag;

@end

プロパティを宣言したら、コンパイラに「セッター」と「ゲッター」を合成させることができます。これらの@property宣言について、最新のコンパイラ (Xcode 4.4 ... は 1 週間ほど前にリリースされました) を使用している場合は@synthesize@implementation. ただし、以前のコンパイラを使用している場合は@synthesize、すべての@property宣言に含める必要があります。

@implementation State

@synthesize forgotPassword = _forgotPassword;
@synthesize categorySelection = _categorySelection;
@synthesize subCategorySelection = _subCategorySelection;
@synthesize string = _string;
// etc.

これを行うと (a を宣言して@propertyからそれを宣言する@synthesize)、コンパイラは舞台裏でインスタンス変数を作成し、自動的に "setter" (つまり、"set" の後に変数名が続くメソッド) を生成します。 「setForgotPassword」) と「getter」メソッド (変数の内容を取得する、変数と同じ名前のメソッド) を各プロパティに使用します。これらすべてのプロパティについて、@synthesize forgotPassword = _forgotPasswordはインスタンス変数も生成しますが、ivar 名の前にアンダースコアを含めることで、プロパティself.forgotPasswordとインスタンス変数を混​​同しないように注意してください_forgotPassword

それをカテゴリにしたい場合 (基本的には、既存のクラスを参照することによって指定され、Stateその後にカテゴリ指定子が続く、既存のクラスに適用される新しいメソッドの追加State (private))、新しい変数を含めることはできません。それが本当にあなたの意図であったかどうかはわかりません(私はそれを疑っています)。しかし、本当にそれを行いたいが、これらの新しい変数が本当に必要な場合は、State次のように代わりに既存のクラスをサブクラス化できます。

@interface StateSubClass : State
{
    NSString *forgotPassword;
    NSMutableArray *categorySelection;
    NSMutableArray *subCategorySelection;
    NSString *string;
    int tag;
    int alertTag;
    NSURL *stringURL;
    NSURL *videoURL;
    NSURL *imageURL;
    int loginCount;
    NSMutableArray *album;
    NSString *videoFileName;
    int videoCounting;
    int loginUserId;
    int imageTag;
}
@end

また、お気付きかもしれませんが、変数名も Apple の最初の小文字の規則に準拠するように変更しました。クラスは大文字で始まり、変数は小文字で始まります。

于 2012-07-30T04:44:55.957 に答える
0

問題は、@interface 自体で変数を初期化していることです。変数のみを宣言でき、初期化は @implementation で行われます。

したがって、 @implementation には、

@implementation

forgotPassword=nil;    
CategorySelection = nil;      
subCategorySelection= nil;   
string = nil;   
Tag= 0;   
alertTag=0;   
stringURL =nil;   
videoURL =nil;   
imageURL = nil;     
loginCount = 0;
videoFileName = nil;    
videoCounting=0;    
loginUserId = 0;    
ImageTagg = 0;    

@end

あなたの @interface が含まれている間、

@interface State       


NSString *forgotPassword;    
NSMutableArray *CategorySelection;    
NSMutableArray *subCategorySelection;     
NSString *string;    
int Tag;    
int alertTag;    
NSURL *stringURL;    
NSURL *videoURL;    
NSURL *imageURL;    
int loginCount;
NSMutableArray *album;    
NSString *videoFileName;    
int videoCounting;    
int loginUserId;    
int ImageTagg;    

@end

privateキーワードが削除されていることに注意してください。objectice C でプライベート変数を宣言するには、.mファイル自体で宣言します。以下に例を示します。

.h ファイル

@interface classname{
// variable declaration
}
//variable property declaration
//method declaration
@end

.m ファイル

@interface classname()
//private variables
@end

@implementation
//do your logic here
@end
于 2012-07-30T05:42:45.627 に答える
0

変数宣言は、次のように括弧内にある必要があります

@interface State(private)
{
NSString *forgotPassword;
NSMutableArray *CategorySelection;
NSMutableArray *subCategorySelection;
NSString *string;
int Tag;
int alertTag;
NSURL *stringURL;
NSURL *videoURL;
NSURL *imageURL;
int loginCount;
NSMutableArray *album;
NSString *videoFileName;
int videoCounting;
int loginUserId;
int ImageTagg;
}
-(void)sampleMethod1;
-(void)sampleMethod2;
-(void)sampleMethod3;
@end

ブラケットの後、クラスで使用しているメソッドを宣言できます

于 2012-07-30T04:40:38.753 に答える