[英]Local declaration hides instance variable - property and synthesize
更新0
您的意思是將所有deals
更改為_deals
並將所有iboards
為_iboards
並添加以下方法,然后在processPbn
方法結束時調用它嗎? 如果是這樣,通話是什么樣的?
- (id)initWithName:(NSInteger )_iboard deals:(NSArray *)_deals
{
self = [super init];
if (self) {
iboard = _iboard;
deals = _deals;
return self;
}
return nil;
}
更新0
我想制作2個變量deals
和iboards
,它們是在BSViewcontroller
中計算的,可以在BSdealViewController
使用。 我在BSViewController.m 的最后 ( 最后3行 )的代碼中得到以下兩個語義問題。
"Local declaration of 'deals' hides instance variable"
"Local declaration of 'iboards' hides instance variable"
BSViewController.h
#import <UIKit/UIKit.h>
@interface BSViewController : UIViewController <....>
{
NSInteger iboard;
NSArray *deals;
}
@property (nonatomic) NSInteger iboard;
@property (nonatomic, strong) NSArray *deals;
- (void) processPbn;
@end
BSViewController.m
#import "BSViewController.h"
@interface BSViewController ()
@end
@implementation BSViewController
@synthesize iboard, deals;
- (void) processPbn
{
NSURLRequest *theRequest = [NSURLRequest .....];
[NSURLConnection sendAsynchronousRequest:theRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *connection, NSData *data, NSError *error)
{
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *sp = @" ";
NSArray*deals=@[[@[sp, sp, sp, sp]mutableCopy],[@[sp, sp, sp, sp]mutableCopy],[@[sp, sp, sp, sp]mutableCopy],[@[sp, sp, sp, sp]mutableCopy],[@[sp, sp, sp, sp]mutableCopy],[@[sp, sp, sp, sp]mutableCopy]];
NSString *toMatch = @"...";
int iboard = 0;
NSRegularExpression *regex = [....];
NSUInteger numberOfMatches = [....];
for (NSTextCheckingResult* board in [.....])
{
for (NSUInteger irange = 1; irange < board.numberOfRanges; ++irange)
{
NSRange matchedRange = [board rangeAtIndex: irange];
NSString* tstring = [string substringWithRange: matchedRange];
for (NSUInteger ix = 0; ix < tstring.length; ++ix)
{
NSRange cardInSuit = NSMakeRange(ix, 1);
int seat = (irange-1)/4 ;
int suit = (irange-1)%4 ;
NSString* replace= [deals[iboard][suit] ....];
[deals[iboard] replaceObjectAtIndex: suit withObject: replace];
}
}
++iboard;
}
}];
}
@end
(由於上述原因?)我也在下面的NSLog指令中收到錯誤Use of undeclared identifier 'iboard'
。
BSdealViewController.m
#import "BSdealViewController.h"
#import "BSViewController.h"
@interface BSdealViewController ()
@end
- (void)viewDidLoad
{
NSLog(@"iboard : %@", iboard);
}
"Local declaration of 'deals' hides instance variable" "Local declaration of 'iboard' hides instance variable"
您的方法中有變量命名的deals
,它與具有相同名稱的ivar發生沖突。
NSString * toMatch = @“......”;
int iboard = 0; ^^^^^^ NSRegularExpression *regex = [....];
也,
NSString *sp = @" ";
NSArray*deals=@[[@[sp, sp, sp, sp]mutableCopy],[@[sp, sp, sp, sp]mutableCopy],[@[sp, sp, sp, sp]mutableCopy],[@[sp, sp, sp, sp]mutableCopy],[@[sp, sp, sp, sp]mutableCopy],[@[sp, sp, sp, sp]mutableCopy]];
^^^^^
將方法名稱中的變量更改為aDeals
和aIboards
或與aIboards
不同的內容。
以下是您應該遵循的大多數有經驗的開發人員使用的約定:
盡可能使用屬性,例如屬性“iboard”和屬性“deals”。 使用帶有前導下划線的實例變量,如“_iboard”和“_deals”。
不要在其他任何地方使用前導下划線。
這樣,讀取源代碼的任何人都可以發現一英里之外的實例變量的使用,並且當您打算使用屬性時,您不會意外地使用實例變量。 你的init方法應該是
- (id)initWithIBoard:(NSInteger )iboard deals:(NSArray *)deals
{
if ((self = [super init]) != nil)
{
_iboard = iboard;
_deals = deals;
}
return self;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.