kevinabraham kevinabraham - 1 year ago 67
iOS Question

How to access elements from an NSObject in swift?

I've set some variables as an Object

import UIKit
class SpeedTestResult: NSObject {
var testTime: NSDate?

Then in the controller I set this object and pass it to a class to store it:

testResult.testTime = NSDate()

I need to store this object and then access the elements within in a view later, This is what I have:

import Foundation
class SpeedTestManager : NSObject {
var testResultArray = [NSObject]()
func addTestResult(testResult: NSObject) {
print("Printing testResultArray: \(testResultArray)")


But when I try to print the the object I just get

Printing testResultArray: [<ProjectName.SpeedTestResult: 0x127b85e50>]

How do I access elements within the object and store this object and retrieve it for later use in a view?

Answer Source

Option 1 NSUserDefault

Notice that not every object is supported NSUserDefault [reference]. You can save an array of your NSDate (testTime in your case). The data will be saved to disc. And when you need them later, you can load them back to memory.

Option 2 CoreData

CoreData is designed for saving and retrieving data for latter use. Depending one how much data your want to save. If you only have what you provide in the question, Option 1 is good enough.