本地声明隐藏实例变量 - 属性和合成

时间:2013-03-10 10:57:52

标签: ios objective-c cocoa

更新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

我想制作dealsiboards的变量BSViewcontrollerBSdealViewController,这些变量在"Local declaration of 'deals' hides instance variable" "Local declaration of 'iboards' hides instance variable" 中计算,可用于#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的末尾(最后3行)的代码中得到以下两个语义问题。

#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

BSViewController.h

Use of undeclared identifier 'iboard'

BSViewController.m

#import "BSdealViewController.h"
#import "BSViewController.h"

@interface BSdealViewController ()

@end
- (void)viewDidLoad
{
    NSLog(@"iboard : %@", iboard);
}

(由于上述原因?)我也在下面的NSLog指令中收到错误{{1}}。

BSdealViewController.m

{{1}}

2 个答案:

答案 0 :(得分:3)

"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或与ivars不同的内容。

答案 1 :(得分:0)

以下是大多数有经验的开发人员应遵循的惯例:

尽可能使用属性,例如属性&#34; iboard&#34;和财产&#34;交易&#34;。 使用带有前导下划线的实例变量,例如&#34; _iboard&#34;和&#34; _deals&#34;。

请勿在其他地方使用前导下划线。

这样,读取源代码的任何人都可以发现一英里之外的实例变量的使用,并且当您打算使用属性时,不会意外地使用实例变量。你的init方法应该是

- (id)initWithIBoard:(NSInteger )iboard deals:(NSArray *)deals
{
    if ((self = [super init]) != nil)
    {
        _iboard = iboard;
        _deals = deals;
    }

    return self;
}