Dumitru Rogojinaru Dumitru Rogojinaru - 27 days ago 20
Objective-C Question

Changing UserDefaultLanguage Swift problems

I am new to programming and I have found how to change the

CLLocation
reverse Geolocation only in Objective C:

NSMutableArray *userDefaultLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"];

[self.geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray* placemarks, NSError* error){
MKPlacemark *placemarker = [placemarks objectAtIndex:0];
NSLog(@"%@",placemarker.locality);
}];

[[NSUserDefaults standardUserDefaults] setObject:userDefaultLanguages forKey:@"AppleLanguages"];


And I have big difficulties to translate it in Swift, this is what I managed to do:

let userDefLang = UserDefaults.standard.array(forKey: "AppleLanguages")
var placeMark: CLPlacemark!
placeMark = placemarks?[0]
if let city = placeMark.addressDictionary!["State"] as? String
{
self.city = city as String
} else
{
self.city = "CONNECTION"
}
if let country = placeMark.addressDictionary!["Country"] as? String
{
self.country = country as String
} else
{
self.country = "FAILED"
}
self.currentCity.name = ("\(self.city), \(self.country)" as String)


but I dunno how properly to set the language and than change it back, please help me... The things I can not make are:

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"];


and

[[NSUserDefaults standardUserDefaults] setObject:userDefaultLanguages forKey:@"AppleLanguages"];


UPDATE

I have made the changes like you told (I wish I understood how) this is the code:

func convertLocationToCity()
{
if let defaultLanguage = UserDefaults.standard.array(forKey: "AppleLanguages") as? [String]
{
let newLang = ["en"]
UserDefaults.standard.set(newLang, forKey: "AppleLanguages")

let geoCoder = CLGeocoder()
let userLocation = CLLocation(latitude: self.lat, longitude: self.long)
geoCoder.reverseGeocodeLocation(userLocation, completionHandler:
{

(placemarks, error) -> Void in

var placeMark: CLPlacemark!
placeMark = placemarks?[0]
var cityRef = ""
var countryRef = ""
if let city = placeMark.addressDictionary!["State"] as? String
{
cityRef = city as String
} else
{
cityRef = ""
}
if let country = placeMark.addressDictionary!["Country"] as? String
{
countryRef = country as String
} else
{
countryRef = ""
}
self.currentCity = ("\(cityRef), \(countryRef)" as String)
if (self.didLoadCity == "" || self.didLoadCity == self.currentCity)
{
self.didLoadCity = ("\(cityRef), \(countryRef)" as String)
}
print("\(self.currentCity)")
self.cityNameLabel.text = self.currentCity
})


//After that set again defaultLanguage.
UserDefaults.standard.set(defaultLanguage, forKey: "AppleLanguages")
}

}


but still no changes, it shows me the response in phone's default language.

Answer

For that you can use array(forKey:) and set(_:forKey:) on UserDefaults object like this.

if let defaultLanguage = UserDefaults.standard.array(forKey: "AppleLanguages") as? [String] {
        let newLang = ["en"]
        UserDefaults.standard.set(newLang, forKey: "AppleLanguages")

        //Perform your Task here

        //After that set again defaultLanguage.
        UserDefaults.standard.set(defaultLanguage, forKey: "AppleLanguages") 
    }
Comments