簡體   English   中英

本地聲明隱藏實例變量 - 屬性和合成

[英]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個變量dealsiboards ,它們是在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]];
        ^^^^^

將方法名稱中的變量更改為aDealsaIboards或與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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM