You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Objective-C通过循环将NSMutableArray数据存入对应NSString变量

How to Extract Fields from NSMutableArray into NSString Variables via For Loop

Hey there! Let's work through this together. First, I need to clarify a common assumption about your data structure—since you've got tabular data (id, money, package, type), your NSMutableArray is almost certainly holding NSDictionary objects, where each dictionary maps those field names to their corresponding values. That's the standard way to structure this kind of data in Objective-C.

Case 1: Array contains NSDictionary objects

If that's the setup, here's a clean, efficient way to loop through the array and pull out each field into dedicated NSString variables for every entry:

// Assume your NSMutableArray is named dataArray
for (NSDictionary *entry in dataArray) {
    // Extract each field into NSString variables
    NSString *idStr = entry[@"id"];
    NSString *moneyStr = entry[@"money"];
    NSString *packageStr = entry[@"package"];
    NSString *typeStr = entry[@"type"];
    
    // Use the variables however you need—example: log them
    NSLog(@"Entry: ID=%@ | Money=%@ | Package=%@ | Type=%@", idStr, moneyStr, packageStr, typeStr);
    
    // If you need to store all values for a single field (e.g., all IDs), you could add them to another array:
    // [allIdsArray addObject:idStr];
}

Breakdown:

  • We use fast enumeration (for...in) to loop through each dictionary in the array—it's the most readable and efficient method for iterating over collections in Objective-C.
  • Subscript syntax (entry[@"key"]) lets us grab the value for each field directly. Since we know these values are strings, we can safely assign them to NSString variables (the compiler handles implicit casting here if your dictionary values are indeed strings).

Case 2: Array contains raw string lines

If your array instead holds raw strings (each element is a full line like @"1 $1 1,020MB Somnet"), we'll need to split each string into components first:

for (NSString *line in dataArray) {
    // Split the line into parts using whitespace as the separator
    NSArray *components = [line componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    // Make sure we have exactly 4 components to avoid crashes
    if (components.count == 4) {
        NSString *idStr = components[0];
        NSString *moneyStr = components[1];
        NSString *packageStr = components[2];
        NSString *typeStr = components[3];
        
        NSLog(@"Parsed line: %@ | %@ | %@ | %@", idStr, moneyStr, packageStr, typeStr);
    } else {
        // Handle malformed lines gracefully
        NSLog(@"Warning: Skipping invalid line format: %@", line);
    }
}

Breakdown:

  • componentsSeparatedByCharactersInSet: splits the string into an array of substrings wherever whitespace occurs.
  • We add a check for components.count == 4 to prevent index-out-of-bounds errors if any line doesn't match the expected format.

内容的提问来源于stack exchange,提问作者Mohamed Ali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:53:59