Multilanguage functionality in swift 3 - ios

I am following below link for multilanguage functionality in my app but when i am changing the language of Finnish i.e "fi" to chineese simplified i.e "zh-Hans" then it is not wokring.
Change Language in the app programmatically
Here is code:
class LanguageManager: NSObject {
var availableLocales = [CustomLocale]()
static let sharedInstance = LanguageManager()
var lprojBasePath = String()
override fileprivate init() {
super.init()
let english = CustomLocale(languageCode: GlobalConstants.englishCode, countryCode: "gb", name: "United Kingdom")
let finnish = CustomLocale(languageCode: GlobalConstants.finnishLangCode, countryCode: "cn", name: "China")
self.availableLocales = [english,finnish]
self.lprojBasePath = getSelectedLocale()
}
fileprivate func getSelectedLocale()->String{
let lang = Locale.preferredLanguages//returns array of preferred languages
let languageComponents: [String : String] = Locale.components(fromIdentifier: lang[0])
if let languageCode: String = languageComponents["kCFLocaleLanguageCodeKey"]{
for customlocale in availableLocales {
if(customlocale.languageCode == languageCode){
return customlocale.languageCode!
}
}
}
return "en"
}
func getCurrentBundle()->Bundle{
if let bundle = Bundle.main.path(forResource: lprojBasePath, ofType: "lproj"){
return Bundle(path: bundle)!
}else{
fatalError("lproj files not found on project directory. /n Hint:Localize your strings file")
}
}
func setLocale(_ langCode:String){
UserDefaults.standard.set([langCode], forKey: "AppleLanguages")//replaces Locale.preferredLanguages
UserDefaults.standard.synchronize()
self.lprojBasePath = getSelectedLocale()
}
}
class CustomLocale: NSObject {
var name:String?
var languageCode:String?
var countryCode:String?
init(languageCode: String,countryCode:String,name: String) {
self.name = name
self.languageCode = languageCode
self.countryCode = countryCode
}
}

Related

Change app language using button programmatically

I'm developing an iOS app with five languages.I have done all the coding and now I'm trying to switch between languages using the button.But can't change the language..I found the problem but cant find the solution.
[NSBundle setLanguage:code];
Problem this line not calling..
Any help please? thanks..
Use localization to change the language of the application without restart.
Go to project -> select your project in the document outline -> Add the new language.
Add Localized.strings files to your project.
Add the strings which are used for localization in Localizable.strings(XXX)
For English "hello" = "Hello World";
For Hindi "hello" = "नमस्ते दुनिया";
Code:
import UIKit
extension String {
var localized: String {
let lang = currentLanguage()
let path = Bundle.main.path(forResource: lang, ofType: "lproj")
let bundle = Bundle(path: path!)
return NSLocalizedString(self, tableName: nil, bundle: bundle!, value: "", comment: "")
}
//Remove here and add these in utilities class
func saveLanguage(_ lang: String) {
UserDefaults.standard.set(lang, forKey: "Locale")
UserDefaults.standard.synchronize()
}
func currentLanguage() -> String {
return UserDefaults.standard.string(forKey: "Locale") ?? ""
}
}
enum Language: String {
case english = "English"
case hindi = "हिंदी"
}
class ViewController: UIViewController {
var language = Language.english
override func viewDidLoad() {
super.viewDidLoad()
//Initial Setup
String().saveLanguage("en")
languageLabel.text = "hello".localized
languageButton.setTitle(language.rawValue, for: .normal)
}
func updateLanguage() {
if language == .english {
String().saveLanguage("hi")
language = .hindi
} else {
String().saveLanguage("en")
language = .english
}
languageLabel.text = "hello".localized
languageButton.setTitle(language.rawValue, for: .normal)
}
#IBOutlet weak var languageLabel: UILabel!
#IBOutlet weak var languageButton: UIButton!
#IBAction func changeLanguageButtonTapped(_ sender: UIButton) {
updateLanguage()
}
}

Implement Google Details in my classes to retrive information about nearby places

I created these two classes to download nearby places and get the details about them (like name, distance, type etc.)
import Foundation
import UIKit
import CoreLocation
import Alamofire
class NearbyPlaces {
static func getCategories() -> [QCategoryy] {
let list:[QCategoryy] = [QCategoryy(name: "bar", image: UIImage(named: "bar_button.png")!), QCategoryy(name :"night_club", image: UIImage(named: "nightclub_button.png")!), QCategoryy(name: "restaurant", image: UIImage(named: "restaurant_button.png")!), QCategoryy(name: "gym", image: UIImage(named: "gym_button.png")!), QCategoryy(name: "spa", image: UIImage(named: "spa_button.png")!), QCategoryy(name: "museum", image: UIImage(named: "museum_button.png")!)]
return list
}
static let searchApiHost = "https://maps.googleapis.com/maps/api/place/nearbysearch/json"
static let googlePhotosHost = "https://maps.googleapis.com/maps/api/place/photo"
static let detailsApiHost = "https://maps.googleapis.com/maps/api/place/details/json"
static func getNearbyPlaces(by category:String, coordinates:CLLocationCoordinate2D, radius:Int, token: String?, completion: #escaping (QNearbyPlacesResponse?, Error?) -> Void) {
var params : [String : Any]
if let t = token {
params = [
"key" : AppDelegate.googlePlacesAPIKey,
"pagetoken" : t,
]
} else {
params = [
"key" : AppDelegate.googlePlacesAPIKey,
"radius" : radius,
"location" : "\(coordinates.latitude),\(coordinates.longitude)",
"type" : category.lowercased()
]
}
Alamofire.request(searchApiHost, parameters: params, encoding: URLEncoding(destination: .queryString)).responseJSON { response in
if let error = response.error {
completion(nil, error)
}
if let response = QNearbyPlacesResponse(dic : response.result.value as? [String : Any]) {
completion(response, nil)
}
else {
completion(nil, QNearbyPlacesResponseError.noParsingDone)
}
}
}
static func googlePhotoURL(photoReference:String, maxWidth:Int) -> URL? {
return URL.init(string: "\(googlePhotosHost)?maxwidth=\(maxWidth)&key=\(AppDelegate.googlePlacesAPIKey)&photoreference=\(photoReference)")
}
}
enum QNearbyPlacesResponseError : Error {
case noParsingDone
}
struct QNearbyPlacesResponse {
var nextPageToken: String?
var status: String = "NOK"
var places: [QPlace]?
init?(dic:[String : Any]?) {
nextPageToken = dic?["next_page_token"] as? String
if let status = dic?["status"] as? String {
self.status = status
}
if let results = dic?["results"] as? [[String : Any]]{
var places = [QPlace]()
for place in results {
places.append(QPlace.init(placeInfo: place))
}
self.places = places
}
}
func canLoadMore() -> Bool {
if status == "OK" && nextPageToken != nil && nextPageToken?.characters.count ?? 0 > 0 {
return true
}
return false
}
}
and also this class
import UIKit
import CoreLocation
import GooglePlaces
private let geometryKey = "geometry"
private let locationKey = "location"
private let latitudeKey = "lat"
private let longitudeKey = "lng"
private let nameKey = "name"
private let openingHoursKey = "opening_hours"
private let openNowKey = "open_now"
private let vicinityKey = "vicinity"
private let typesKey = "types"
private let photosKey = "photos"
private let ratingKey = "rating"
private let urlKey = "url"
private let priceLevelKey = "price_level"
private let websiteKey = "website"
private let place_idKey = "place_id"
class QPlace: NSObject {
var location: CLLocationCoordinate2D?
var name: String?
var photos: [QPhoto]?
var vicinity: String?
var isOpen: Bool?
var types: [String]?
var rating: Float?
var url: String?
var price_level: Int?
var website: String?
var place_id: String?
init(placeInfo:[String: Any]) {
// coordinates
if let g = placeInfo[geometryKey] as? [String:Any] {
if let l = g[locationKey] as? [String:Double] {
if let lat = l[latitudeKey], let lng = l[longitudeKey] {
location = CLLocationCoordinate2D.init(latitude: lat, longitude: lng)
}
}
}
// name
name = placeInfo[nameKey] as? String
// opening hours
if let oh = placeInfo[openingHoursKey] as? [String:Any] {
if let on = oh[openNowKey] as? Bool {
isOpen = on
}
}
// vicinity
vicinity = placeInfo[vicinityKey] as? String
// types
types = placeInfo[typesKey] as? [String]
// rating
rating = placeInfo[ratingKey] as? Float
url = placeInfo[urlKey] as? String
//priceLevel
price_level = placeInfo[priceLevelKey] as? Int
//website
website = placeInfo[websiteKey] as? String
place_id = placeInfo[place_idKey] as? String
// photos
photos = [QPhoto]()
if let ps = placeInfo[photosKey] as? [[String:Any]] {
for p in ps {
photos?.append(QPhoto.init(photoInfo: p))
}
}
}
func getDescription() -> String {
var s : [String] = []
if let types = types {
s.append("\(types.joined(separator: ", "))")
}
if let rating = rating {
s.append("Rating: \(rating)")
}
if let price_level = price_level {
s.append("Price level: \(price_level)")
}
if let isOpen = isOpen {
s.append(isOpen ? "OPEN NOW" : "CLOSED NOW")
}
if let url = url {
s.append("\(url)")
}
if let website = website {
s.append("\(website)")
}
if let vicinity = vicinity {
s.append("\(vicinity)")
}
return s.joined(separator: "\n")
}
func getDescriptionMaps() -> String {
var m : [String] = []
if let vicinity = vicinity {
m.append("\(vicinity)")
}
return m.joined(separator: "\n")
}
func heightForComment(_ font: UIFont, width: CGFloat) -> CGFloat {
let desc = getDescription()
let rect = NSString(string: desc).boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
return ceil(rect.height)
}
}
my only problem is that i can not download website and price level of the places, so looked in the documentation of google places i found out that my problem is caused because searhAPI (https://developers.google.com/places/web-service/search) does not retrive these information about places, so i have to use https://developers.google.com/places/web-service/details to get place's website and price level, but i need an help because i don't know how to do or how i have to modify my classes to make all this work and able my Class QPlace to get also website and price level of the places.

How to save and fetch data from file in swift?

I want to save the response from JSON in a file and fetch from it when the network is not available. However on trying to fetch idea by disabling the wifi, the app always crashes. Are there any other ways for offline fetching in swift except saving in database??
This is the error I am getting : Could not cast value of type 'Swift._NSContiguousString' (0x109e22320) to 'NSArray'
This is what I have done so far:
Create a model
class Directory : NSObject, NSCoding {
var data : [AnyObject]
var tid : String
var vid : String
var name : String
var imgThumbnail : String
var imgMedium : String
var imgLarge : String
var child : String
// MARK: Archiving Paths
init(data:[AnyObject],tid:String,vid:String,name:String,imgThumbnail:String,imgMedium:String,imgLarge:String,child:String) {
self.data = data ?? []
self.tid = tid ?? ""
self.vid = vid ?? ""
self.name = name ?? ""
self.imgThumbnail = imgThumbnail ?? ""
self.imgMedium = imgMedium ?? ""
self.imgLarge = imgLarge ?? ""
self.child = child ?? ""
}
// MARK: NSCoding
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(data, forKey:Constants.kData)
aCoder.encodeObject(name, forKey:Constants.Directory.kName )
aCoder.encodeObject(tid, forKey: Constants.Directory.tid)
aCoder.encodeObject(vid, forKey: Constants.Directory.vid)
aCoder.encodeObject(imgThumbnail, forKey:Constants.Directory.kImageThumbnail)
aCoder.encodeObject(imgMedium, forKey: Constants.Directory.kImageMedium)
aCoder.encodeObject(imgLarge, forKey: Constants.Directory.kImageLarge)
aCoder.encodeObject(child, forKey: Constants.Directory.kChild)
}
required convenience init?(coder aDecoder: NSCoder) {
let data = aDecoder.decodeObjectForKey(Constants.kData) as! [AnyObject]
let name = aDecoder.decodeObjectForKey(Constants.Directory.kName) as! String
let tid = aDecoder.decodeObjectForKey(Constants.Directory.tid) as! String
let vid = aDecoder.decodeObjectForKey(Constants.Directory.vid) as! String
let imgThumbnail = aDecoder.decodeObjectForKey(Constants.Directory.kImageThumbnail) as! String
let imgMedium = aDecoder.decodeObjectForKey(Constants.Directory.kImageMedium) as! String
let imgLarge = aDecoder.decodeObjectForKey(Constants.Directory.kImageLarge) as! String
let child = aDecoder.decodeObjectForKey(Constants.Directory.kChild) as! String
// Must call designated initializer.
self.init(data:data,tid:tid,vid:vid,name:name,imgThumbnail:imgThumbnail,imgMedium: imgMedium,imgLarge: imgLarge, child: child)
}
}
Code for saving and loading the data from file
class func loadSavedFile(fileName: String) -> AnyObject? {
let pathString: String = Utility.fetchFilePathString(fileName)
print("Here the pathString is \(pathString)")
if NSFileManager.defaultManager().fileExistsAtPath(pathString) {
return NSKeyedUnarchiver.unarchiveObjectWithFile(pathString)!
} else {
return "File doesn't exist"
}
return ""
}
class func saveObject(object: AnyObject, toFile fileName: String) {
let pathString: String = Utility.fetchFilePathString(fileName)
NSKeyedArchiver.archiveRootObject(object, toFile: pathString)
}
class func fetchFilePathString(fileName: String) -> String {
let pathAray: [AnyObject] = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.AllDomainsMask, true)
let pathString = pathAray.last!
return NSURL(fileURLWithPath: pathString as! String).URLByAppendingPathComponent(fileName).absoluteString
}
Checking for network connection in the view controller
var directoryArr = [Directory]()
override func viewDidLoad() {
super.viewDidLoad()
if Utility.isNetworkReachable() {
Utility.saveObject([], toFile: Constants.File.kDirectory)
self.serviceCallDirectory()
} else {
self.directorie = (Utility.loadSavedFile(Constants.File.kDirectory) as? [Directory])!
self.tableView.reloadData()
}
Service Call
func serviceCallDirectory() -> Void {
let stringUrl = Constants.baseUrl + Constants.kDirectoryUrl
WebService.getRequestAPI(stringUrl, withSuccess: {(responseDic, Statusflag,error) in
if Statusflag {
self.tableView.backgroundColor = UIColor.clearColor()
self.tableView.hidden = false
let tempInfo = responseDic![Constants.kData] as! [AnyObject]
var imgthumbnail : String = ""
var imgmedium : String = ""
var imglarge : String = ""
var name : String = ""
var child : String = ""
if tempInfo.count != 0 {
for info in tempInfo {
let tid = info[Constants.Directory.tid] as! String
let vid = info[Constants.Directory.vid] as! String
if let names = info[Constants.Directory.kName] as? String {
name = names
}
if let childs = info[Constants.Directory.kChild] as? String {
child = childs
}
if let imgthumb = info[Constants.Directory.kImageThumbnail] as? String {
imgthumbnail = imgthumb
} else {
imgthumbnail = ""
}
if let imgmediumd = info[Constants.Directory.kImageMedium] as? String {
imgmedium = imgmediumd
} else {
imgmedium = ""
}
if let imglarges = info[Constants.Directory.kImageLarge] as? String {
imglarge = imglarges
}
let myModel = Directory(
data: tempInfo,
tid: tid,
vid: vid,
name: name,
imgThumbnail: imgthumbnail,
imgMedium: imgmedium,
imgLarge: "",
child: child
)
self.directorie.append(myModel)
}
I don't know that this is the only issue, but this code
class func loadSavedFile(fileName: String) -> AnyObject? {
let pathString: String = Utility.fetchFilePathString(fileName)
print("Here the pathString is \(pathString)")
if NSFileManager.defaultManager().fileExistsAtPath(pathString) {
return NSKeyedUnarchiver.unarchiveObjectWithFile(pathString)!
} else {
return "File doesn't exist"
}
return ""
}
Either returns an object or a string. That's not very sensible. It should return a success flag or a tuple or use a completion block. When you call this function your code expects to get back an array of directory, which in a number of cases won't happen
self.directorie = (Utility.loadSavedFile(Constants.File.kDirectory) as? [Directory])!
The error in your question indicates a different kind of data mismatch. You should try not to use AnyObject, let swift help you by type checking what you're doing...

Thread 1:EXC_BAD_INSTRUCTION error when calling "addObjectsFromArray"method for NSMutableArray

I have created a program where it calculates the borders of certain countries based on a Custom Object being inputed into a function. This function returns an array of custom objects. I keep getting this error when trying to add this array to a NSMutableArray. The error is called "Thread 1: EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP, subcode=0x0)".
Here is my adding code:
#IBOutlet var Label: UILabel!
#IBOutlet var imageView: UIImageView!
override func viewDidLoad() {
//Afganistan
Afghanistan.name = "Afghanistan"
Afghanistan.borders.addObjectsFromArray(relate(Afghanistan))
Label.text = String((Afghanistan.borders[0] as! develop).name)
}
Here is the relate method along with the group dictionary:
func relate(X : develop) -> Array<develop>{
var A : [develop] = []
for (_, value) in groups {
for y in value {
if y.name == X.name {
for i in value {
if i.name != X.name {
A.append(i)
}
}
}
}
}
return A
}
//Groups
var groups = [
"one": [Afghanistan]
]
Here is the class of develop:
class develop : NSObject, NSCoding {
//Shared
var power : Int!
var name : String!
var image : UIImage!
var flag : UIImage!
var militaryName : String!
//Military
var experience : Int!
var OATC : Int!
var navy : Int!
var airforce : Int!
var artillery : Int!
//Geography
var borders : NSMutableArray!
var cities : NSMutableArray!
//Biomes
var biom : NSMutableArray!
var continent : String!
var NSEW : String!
//Encoding Section
required convenience init(coder decoder: NSCoder) {
self.init()
//shared
self.power = decoder.decodeObjectForKey("power") as! Int
self.name = decoder.decodeObjectForKey("name") as! String
self.image = decoder.decodeObjectForKey("image") as! UIImage
self.flag = decoder.decodeObjectForKey("flag") as! UIImage
self.militaryName = decoder.decodeObjectForKey("militaryName") as! String
//Military
self.experience = decoder.decodeObjectForKey("experience") as! Int
self.OATC = decoder.decodeObjectForKey("OATC") as! Int
self.navy = decoder.decodeObjectForKey("navy") as! Int
self.airforce = decoder.decodeObjectForKey("airforce") as! Int
self.artillery = decoder.decodeObjectForKey("artillery") as! Int
//Geography
self.borders = decoder.decodeObjectForKey("borders") as! NSMutableArray
self.cities = decoder.decodeObjectForKey("cities") as! NSMutableArray
self.continent = decoder.decodeObjectForKey("continent") as! String
self.NSEW = decoder.decodeObjectForKey("NSEW") as! String
}
convenience init(
power: Int, name: String, image: UIImage, flag: UIImage,
relations: NSMutableArray, mName: String, experience: Int,
OATC: Int, navy: Int, airforce: Int, artillery: Int,
borders: NSMutableArray, cities: NSMutableArray,
biom: NSMutableArray, continent: String, NSEW: String )
{
self.init()
//shared
self.power = power
self.name = name
self.image = image
self.flag = flag
self.militaryName = mName
//military
self.experience = experience
self.OATC = OATC
self.navy = navy
self.airforce = airforce
self.artillery = artillery
//geography
self.borders = borders
self.cities = cities
self.biom = biom
self.continent = continent
self.NSEW = NSEW
}
func encodeWithCoder(coder: NSCoder) {
//Shared
if let power = power { coder.encodeObject(power, forKey: "power") }
if let name = name { coder.encodeObject(name, forKey: "name") }
if let image = image { coder.encodeObject(image, forKey: "image") }
if let flag = flag { coder.encodeObject(flag, forKey: "flag") }
if let militaryName = militaryName { coder.encodeObject(militaryName, forKey: "militaryName") }
//Military
if let experience = experience { coder.encodeObject(experience, forKey: "experience") }
if let OATC = OATC { coder.encodeObject(OATC, forKey: "OATC") }
if let navy = navy { coder.encodeObject(navy, forKey: "navy") }
if let airforce = airforce { coder.encodeObject(airforce, forKey: "airforce") }
if let artillery = artillery { coder.encodeObject(artillery, forKey: "artillery") }
//geography
if let borders = borders { coder.encodeObject(borders, forKey: "borders") }
if let cities = cities { coder.encodeObject(cities, forKey: "cities") }
if let biom = biom { coder.encodeObject(biom, forKey: "biom") }
if let continent = continent { coder.encodeObject(continent, forKey: "continent") }
if let NSEW = NSEW { coder.encodeObject(NSEW, forKey: "NSEW") }
}
}
"Afghanistan" is a subclass of develop

NSUserDefaults Custom object - Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType')

I must be incorrectly using Custom Objects for NSUserDefaults. The error " Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType')". Below is my code, the Goal class is of particular interest, since this is where I am adopting the NSCoding protocol.
This code is global.
func saveGoals (goals : [Goal]) {
var updatedGoals = NSKeyedArchiver.archivedDataWithRootObject(goals)
NSUserDefaults.standardUserDefaults().setObject(updatedGoals, forKey: "Goals")
NSUserDefaults.standardUserDefaults().synchronize()
}
func loadCustomObjectWithKey() -> [Goal] {
if let encodedObject : NSData = NSUserDefaults.standardUserDefaults().objectForKey("Goals") as? NSData {
var encodedObject : NSData? = NSUserDefaults.standardUserDefaults().objectForKey("Goals") as? NSData
var goal : [Goal] = NSKeyedUnarchiver.unarchiveObjectWithData(encodedObject!) as [Goal]
return goal
} else {
return [Goal]()
}
}
This code is in GoalsViewController.
class GoalsViewController: MainPageContentViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var tableView: GoalsTableView!
var cell = GoalTableViewCell()
var goalsArray : Array<Goal> = [] //
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
if var storedGoals: [Goal] = loadCustomObjectWithKey() as [Goal]? {
goalsArray = storedGoals
}
//retrieve data.
var goal = Goal(title: "Walk the Dog")
goalsArray.append(goal)
saveGoals(goalsArray)
self.tableView?.reloadData()
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
var notification = NSNotificationCenter.defaultCenter()
notification.addObserver(self, selector: "finishCreatingGoal:", name: "FinishCreatingGoal", object: nil)
}
func finishCreatingGoal(notification : NSNotification) {
if (notification.name == "FinishCreatingGoal") {
var userInfo = notification.userInfo!
var text = userInfo["text"]! as String
var index = userInfo["index"]! as Int
var cell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: index, inSection: 0)) as GoalTableViewCell
goalsArray[index].title = cell.goalTextField.text
saveGoalList(goalsArray)
self.tableView.reloadData()
self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: index, inSection: 0), atScrollPosition: UITableViewScrollPosition.Middle, animated: true)
}
}
This code is in the Goal class.
import UIKit
class Goal : NSObject, NSCoding {
var title : String? = ""
var checkmarked : Bool? = false
var isLastCell : Bool? = false
var enabled : Bool? = true
var priority = Priority.defaultPriority
override init() {
}
init(title : String) {
self.title = title
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(title!, forKey: "title")
aCoder.encodeBool(checkmarked!, forKey: "checkmarked")
aCoder.encodeBool(isLastCell!, forKey: "isLastCell")
aCoder.encodeBool(enabled!, forKey: "enabled")
}
required init(coder aDecoder: NSCoder) {
title = aDecoder.decodeObjectForKey("title") as String!
checkmarked = aDecoder.decodeBoolForKey("checkmarked") as Bool
isLastCell = aDecoder.decodeBoolForKey("isLastCell") as Bool
enabled = aDecoder.decodeBoolForKey("enabled") as Bool
}
}
I am going to just copy code from a working project I have:
here is the Game object class with data from a math flash card game:
import Foundation
class GameData: NSObject {
var sign: String = "+"
var level: Int = 1
var problems: Int = 10
var time: Int = 30
var skipWrong: Bool = true
var usedTime: Int = 0
var correctCount: Int = 0
var correctTopNumber: [Int] = [Int]()
var correctBottomNumber: [Int] = [Int]()
var wrongTopNumber: [Int] = [Int]()
var wrongBottomNumber: [Int] = [Int]()
var date: NSDate = NSDate()
func encodeWithCoder(aCoder: NSCoder!) {
aCoder.encodeObject(sign, forKey: "sign")
aCoder.encodeInteger(level, forKey: "level")
aCoder.encodeInteger(problems, forKey: "problems")
aCoder.encodeInteger(time, forKey: "time")
aCoder.encodeBool(skipWrong, forKey: "skipWrong")
aCoder.encodeInteger(usedTime, forKey: "usedTime")
aCoder.encodeInteger(correctCount, forKey: "correctCount")
aCoder.encodeObject(correctTopNumber, forKey: "correctTopNumber")
aCoder.encodeObject(correctBottomNumber, forKey: "correctBottomNumber")
aCoder.encodeObject(wrongTopNumber, forKey: "wrongTopNumber")
aCoder.encodeObject(wrongBottomNumber, forKey: "wrongBottomNumber")
aCoder.encodeObject(date, forKey: "date")
}
init(coder aDecoder: NSCoder!) {
sign = aDecoder.decodeObjectForKey("sign") as String
level = aDecoder.decodeIntegerForKey("level")
problems = aDecoder.decodeIntegerForKey("problems")
time = aDecoder.decodeIntegerForKey("time")
skipWrong = aDecoder.decodeBoolForKey("skipWrong")
usedTime = aDecoder.decodeIntegerForKey("usedTime")
correctCount = aDecoder.decodeIntegerForKey("correctCount")
correctTopNumber = aDecoder.decodeObjectForKey("correctTopNumber") as Array
correctBottomNumber = aDecoder.decodeObjectForKey("correctBottomNumber") as Array
wrongTopNumber = aDecoder.decodeObjectForKey("wrongTopNumber") as Array
wrongBottomNumber = aDecoder.decodeObjectForKey("wrongBottomNumber") as Array
date = aDecoder.decodeObjectForKey("date") as NSDate
}
override init() {
}
}
This part looks about the same as yours, but with more variable types. The archiver and retriever classes differ from you:
import Foundation
class ArchiveGameData:NSObject {
var documentDirectories:NSArray = []
var documentDirectory:String = ""
var path:String = ""
func ArchiveResults(#dataSet: [GameData]) {
documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
documentDirectory = documentDirectories.objectAtIndex(0) as String
path = documentDirectory.stringByAppendingPathComponent("results3.archive")
if NSKeyedArchiver.archiveRootObject(dataSet, toFile: path) {
//println("Success writing to file!")
} else {
println("Unable to write to file!")
}
}
func RetrieveGameData() -> NSObject {
var dataToRetrieve = [GameData]()
documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
documentDirectory = documentDirectories.objectAtIndex(0) as String
path = documentDirectory.stringByAppendingPathComponent("results3.archive")
if let dataToRetrieve2 = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? [GameData] {
dataToRetrieve = dataToRetrieve2
}
return(dataToRetrieve)
}
}
Finally, the code for storing and retrieving from within a ViewController:
//retrieveing
var gameDataArray = ArchiveGameData().RetrieveGameData() as [GameData]
//Archiving
gameData = GameData() //create local object then append all the new data, then store it
gameData.sign = buttonStates.sign
gameData.level = buttonStates.level
gameData.problems = buttonStates.problems
gameData.time = buttonStates.time
//etc. for all properties
ArchiveGameData().ArchiveResults(dataSet: gameDataArray)

Resources