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

  • Home
  • SEARCH
  • 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 864531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:29:37+00:00 2026-05-15T09:29:37+00:00

I have an NSDictionary with NSStrings Some of the valueForKey:@Key have no entry so

  • 0

I have an NSDictionary with NSStrings

Some of the valueForKey:@”Key” have no entry so it’s (null)

NSMutableString* addressDictionaryToString = [NSMutableString string];  // use mutable string!
for (NSDictionary* item in address) {     // use fast enumeration!
        [addressDictionaryToString appendFormat:@"%@, %@, %@, %@",
         [item objectForKey:@"Street"],         
         [item objectForKey:@"City"],
         [item objectForKey:@"State"],
         [item objectForKey:@"ZIP"]
         ];
    NSLog(@"MutableString: %@", addressDictionaryToString);
}

So I want to build an NSMutableString but filter out those keys that are null. Any ideas?

UPDATE:::::

Basically I want my resulting String to look like

1 Infinite Loop, Cupertino, CA, 95014 (IF all fields are available)

If Im missing the Street then

Cupertino, CA, 95014

If I’m missing the State then

1 Infinite Loop, Cupertino, 95014

If I only have the state then it should print

CA

(notice that there are no commas on the last element)

  • 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-15T09:29:38+00:00Added an answer on May 15, 2026 at 9:29 am

    How about checking if there’s a value for the key?

    NSMutableString * addressDictionaryToString = [NSMutableString string];
    for (NSDictionary * item in address)
    {
        if ([item objectForKey:@"Street"])
            [addressDictionaryToString appendFormat:@"%@, ", 
             [item objectForKey:@"Street"]];
        if ([item objectForKey:@"City"])
            [addressDictionaryToString appendFormat:@"%@, ", 
             [item objectForKey:@"City"]];
        if ([item objectForKey:@"State"])
            [addressDictionaryToString appendFormat:@"%@, ", 
             [item objectForKey:@"State"]];
        if ([item objectForKey:@"ZIP"])
            [addressDictionaryToString appendFormat:@"%@, ", 
             [item objectForKey:@"ZIP"]];
        NSLog(@"MutableString: %@", addressDictionaryToString);
    }
    

    The thing is, in your last question, you said your goal was to create a CSV file. It’s not technically valid CSV if your rows have varying numbers of fields with no reliable way to identify each one.

    Instead, you might try this:

    NSMutableString * addressDictionaryToString = [NSMutableString string];
    for (NSDictionary * item in address)
    {
        [addressDictionaryToString appendFormat:@"%@,", 
         ([item objectForKey:@"Street"]) ? [item objectForKey:@"Street"] : @"" ];
        // ...
        NSLog(@"MutableString: %@", addressDictionaryToString);
    }
    

    It checks for the presence of a value and inserts that value if there is one, or just an empty string (resulting in “value,value,,value…”). Also remember there shouldn’t be a space after the comma, so I’ve removed that from this example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How I must remove objects? For example, I have NSDictionary and some NSStrings from
I have some NSDictionary objects stored in an NSArray called telephoneArray . I fetch
I have some python code for Google App Engine that responds with the string
I want to write some values to an external file and use NSDictionary for
I am parsing some JSON values. I have a NSDictionary and for a certain
I have an NSDictionary and I am trying to pull a string out of
I have some code that looks like this: NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions
I have gone through following question. Convert NSString to NSDictionary It is something different
i have a NSDictionary of this type : ********** = ( 20110330 7Huge Sandstorm
I have a NSDictionary called data and when I print using NSLog it gives

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.