0
votes

I'm working on iOS programming using swift. I'm taking a location string address input from a user and returning geocoding results for him to choose from. The problem I have is that when I want to show the user the address I want to show him a full address(name,city,country) or (city,country). So I checked and found the ABCreateStringWithAddressDictionary function was the standard to getting a formatted address. However, using that function is only giving me the first name of the location (the name in case of a name,city,country) location

cell.textLabel?.text = ABCreateStringWithAddressDictionary(returnedQueryLocations[indexPath.row].addressDictionary, true).

This is not returning a full address. Found another solution online

var lines : AnyObject = returnedQueryLocations[indexPath.row].addressDictionary[ "FormattedAddressLines"]!
    var addressString : NSString = lines.componentsJoinedByString(", ")

This was in objective C but I translated it to swift. It works to some extent but it gives me an error in the case of some addresses. Is there another way to tackle this? Is ABCreateStringWithAddressDictionary supposed to give such a result?

Thanks,

Edit: for example, its giving me (Hamra Street) when I search for Hamra,beirut,lebanon the second method gives me the required address(Hamra Street, Beirut, Lebanon) .

1

1 Answers

0
votes

The string returned by ABCreateStringWithAddressDictionary() separates its components using newlines. The text field is only displaying the first line. Before setting the text field, replace the newlines with commas:

var addressString = ABCreateStringWithAddressDictionary(returnedQueryLocations[indexPath.row].addressDictionary, true)
addressString = addressString.stringByReplacingOccurrencesOfString("\n", withString: ", ")
cell.textLabel?.text = addressString