Coder Social home page Coder Social logo

Comments (2)

austin-bowen-advyon avatar austin-bowen-advyon commented on May 18, 2024

Okay, I just wrote one.

APAddress.h:
- (NSString *) toString;

APAddress.m:

- (NSString *) toString
{
    BOOL hasStreet      = (self.street      && [self.street length]);
    BOOL hasCity        = (self.city        && [self.city length]);
    BOOL hasState       = (self.state       && [self.state length]);
    BOOL hasZip         = (self.zip         && [self.zip length]);
    BOOL hasCountry     = (self.country     && [self.country length]);
    BOOL hasCountryCode = (self.countryCode && [self.countryCode length]);
    BOOL countryCodeIsNumeric = ([[NSNumberFormatter new] numberFromString:self.countryCode] != nil);

    NSMutableString *str = [NSMutableString new];

    // Street
    if (hasStreet)
    {
        [str appendString:[self.street capitalizedString]];
        [str appendString:@", "];
    }

    // City
    if (hasCity)
    {
        [str appendString:[self.city capitalizedString]];
        [str appendString:@", "];
    }

    // State
    if (hasState)
    {
        if ([self.state length]) > 2)
            [str appendString:[self.state capitalizedString]];
        else
            [str appendString:[self.state uppercaseString]];
        if (hasZip) [str appendString:@" "];
        else        [str appendString:@", "];
    }

    // Zip
    if (hasZip)
    {
        [str appendString:self.zip];
        [str appendString:@", "];
    }

    // Country
    if (hasCountryCode && !countryCodeIsNumeric)
        [str appendString:[self.countryCode uppercaseString]];
    else if (hasCountry)
        [str appendString:[self.country capitalizedString]];

    // Clean up the string
    [str setString:[str stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@", \t\n"]]];

    return [NSString stringWithString:str];
}

from apaddressbook.

belkevich avatar belkevich commented on May 18, 2024

Looks good but in different countries different address formats. So, it's not suitable for all.
You can create category on APContact and use it your projects

from apaddressbook.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.