简体   繁体   中英

NSMutableArray NSLog outputs different results

I'm trying to assign tags and then creating an NSMutableArray

#import "myClass"

static NSString *kC = @"100";
static NSString *kLo = @"110";

@interface MyApp()
@property (strong, nonatomic) NSMutableArray *arrayTag;
@end

@Synthesize arrayTag;

in viewDidLoad

arrayTag = [[NSMutableArray alloc] init];


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.showsReorderControl = YES;
    }

    if (indexPath.row == 0)
    {
        cell.tag = kC;
            NSString *cTag = [NSString stringWithFormat:@"%i", cell.tag];
            if (![arrayTag containsObject:cTag])
            {
                [arrayTag addObject:cTag];
            }
    }

    if (indexPath.row == 1)
    {
         cell.tag = kLo;
            NSString *loTag = [NSString stringWithFormat:@"%i", cell.tag];
            if (![arrayTag containsObject:loTag])
            {
                [arrayTag addObject:loTag];
            }
    }
return cell;
}

To NSLog :

- (IBAction)doneButton:(id)sender
{

    NSLog (@"Number of Objects in Array %i", arrayTag.count);

    NSLog (@"Object at Index 0 in Array %@", [arrayTag objectAtIndex:0]);
    NSLog (@"Object at Index 1 in Array %@", [arrayTag objectAtIndex:1]);

    for (NSString *obj in arrayTag){
        NSLog(@"From ArrayTag obj: %@", obj);
    }
}

Here is the Log

2013-07-10 15:46:39.468 MyApp[1013:c07] Number of Objects in Array 2
2013-07-10 15:46:39.469 MyApp[1013:c07] Object at Index 0 in Array 1624272
2013-07-10 15:46:39.469 MyApp[1013:c07] Object at Index 1 in Array 1624256
2013-07-10 15:46:39.469 MyApp[1013:c07] From ArrayTag obj: 1624272
2013-07-10 15:46:39.470 MyApp[1013:c07] From ArrayTag obj: 1624256

Q: Shouldn't the value of Object[0] = 100 and Object[1] = 110 to match cell.tag ? Why does it show up 1624272 and 1624256?

UIView's tag property expects an NSInteger , you are assigning a NSString , or to be more precise, the memory address of the NSString to the tag.

Use something like this:

static NSInteger kC = 100;
static NSInteger kLo = 110;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM