1

更新 0

您的意思是更改所有dealsto_deals和所有iboardsto_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和可在 中使用。我在BSViewController.m的末尾(最后 3 行)的代码中得到以下两个语义问题。iboardsBSViewcontrollerBSdealViewController

"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

(由于上述原因?)我Use of undeclared identifier 'iboard'在下面的 NSLog 指令中也遇到了错误。

BSdealViewController.m

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

@interface BSdealViewController ()

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

2 回答 2

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不同的东西。

于 2013-03-10T11:00:24.253 回答
0

这是您应该遵循的大多数有经验的开发人员使用的约定:

尽可能使用属性,例如属性“iboard”和属性“deals”。使用带有前导下划线的实例变量,例如“_iboard”和“_deals”。

不要在其他任何地方使用前导下划线。

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

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

    return self;
}
于 2014-06-25T10:55:36.930 回答