Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4021422
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:20:59+00:00 2026-05-20T10:20:59+00:00

I am using a for loop to (currently) NSLog the contents of a NSArray.

  • 0

I am using a for loop to (currently) NSLog the contents of a NSArray. However I would like to set the contents of the array into a NSMutableDictionary, depending on the objectAtIndex it is. Currently there are 843 objects in the array, and therefore I would rather not have to type out the same thing over and over again!

My code currently is this

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSArray *chunks = [string componentsSeparatedByString:@","];
for (int i = 0; i < [chunks count]; i ++) {
    NSLog(@"%@", [chunks objectAtIndex:i]);
}

I would like to set the contents of the array into the NSMutableDictionary in the following fashion, and once the objectAtIndex is 11, I would like to set the 12th object in the dictionary to be of the key @”type” and soforth:

[dict setObject:[chunks objectAtIndex:0] forKey:@"type"];
[dict setObject:[chunks objectAtIndex:1] forKey:@"name"];
[dict setObject:[chunks objectAtIndex:2] forKey:@"street"];
[dict setObject:[chunks objectAtIndex:3] forKey:@"address1"];
[dict setObject:[chunks objectAtIndex:4] forKey:@"address2"];
[dict setObject:[chunks objectAtIndex:5] forKey:@"town"];
[dict setObject:[chunks objectAtIndex:6] forKey:@"county"];
[dict setObject:[chunks objectAtIndex:7] forKey:@"postcode"];
[dict setObject:[chunks objectAtIndex:8] forKey:@"number"];
[dict setObject:[chunks objectAtIndex:9] forKey:@"coffee club"];
[dict setObject:[chunks objectAtIndex:10] forKey:@"latitude"];
[dict setObject:[chunks objectAtIndex:11] forKey:@"longitude"];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:21:00+00:00Added an answer on May 20, 2026 at 10:21 am

    I’m not sure I fully understand the question, but I think that your chunks array contains a long list of data, ordered in the same way (i.e. 0th, 12th, 24th, 36th… elements are all type, and 1st, 13th, 25th, 37th… elements are all name). If this is the case, you could use something like this:

    NSArray *keys = [NSArray arrayWithObjects:@"type", @"name", @"street", @"address1", @"address2", @"town", @"county", @"postcode", @"number", @"coffee club", @"latitude", @"longitude", nil];
    
    for (NSUInteger i = 0; i < [chunks count]; i += [keys count])
    {
        NSArray *subarray = [chunks subarrayWithRange:NSMakeRange(i, [keys count])];
        NSDictionary *dict = [[NSDictionary alloc] initWithObjects:subarray forKeys:keys];
        
        // do something with dict
    
        [dict release];
    }
    

    Note that you can’t have two different values for the same key with NSDictionary. That is, if you set two different values for the type key, only the last value set will be kept.

    Edit

    If your array is not a multiple of 12 because for example it contains garbage data at the end, you could use a different looping style instead:

    // max should be a multiple of 12 (number of elements in keys array)
    NSUInteger max = [chunks count] - ([chunks count] % [keys count]);
    NSUInteger i = 0;
    
    while (i < max)
    {
        NSArray *subarray = [chunks subarrayWithRange:NSMakeRange(i, [keys count])];
        NSDictionary *dict = [[NSDictionary alloc] initWithObjects:subarray forKeys:keys];
        
        // do something with dict
    
        [dict release];
        
        i += [keys count];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I cannot seem to access the context object using a loop context is set:
I'm using reflection to loop through a Type 's properties and set certain types
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I am using XmlReader in .NET to parse an XML file using a loop:
Is there any way to clean up this type of loop using LINQ? List<Car>
I just want a quick way (and preferably not using a while loop)of createing
How can I iterate over each file in a directory using a for loop?
I'm using the following code to loop through a directory to print out the
I'm using the following syntax to loop through a list collection: For Each PropertyActor
I have a loop that reads each line in a file using getline() :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.