nscoding

NSCoding with Nested Custom Objects?

前提是你 提交于 2019-12-19 05:22:15
问题 I have a series of nested objects that I am needing to put through the NSCoding protocol so that I can save the top level object into NSUserDefaults. Here is the structure of objects: 'Instructor' class NSMutableArray that holds instances of... 'Class' class NSMutableArray that holds instances of... 'Student' class Name Property Number Property Money Property I am needing to save an instance of Instructor to NSUserDefaults or to documents for the app. As you can see the Instructor object is

Save struct in class to NSUserDefaults using Swift

蓝咒 提交于 2019-12-18 12:29:45
问题 I have a class and inside the class is a (swift) array, based on a global struct. I want to save an array with this class to NSUserDefaults. This is my code: struct mystruct { var start : NSDate = NSDate() var stop : NSDate = NSDate() } class MyClass : NSObject { var mystructs : [mystruct] init(mystructs : [mystruct]) { self.mystructs = mystructs super.init() } func encodeWithCoder(encoder: NSCoder) { //let val = mystructs.map { $0 as NSObject } //this also doesn't work let objctvtmrec =

objects conforming to nscoding will not writetofile

大兔子大兔子 提交于 2019-12-18 09:05:12
问题 I want to write an array of Question objects to file, but somehow writeToFile isn't doing anything. A Question has an Owner and and an array of Answer objects. An Answer has an Owner as well. All three of them conform to the NSCoding protocol (as far as I know). From the code below, result returns NO. No clue what I'm doing wrong, since I am implementing NSCoding for everything, right? Question.h #import <Foundation/Foundation.h> #import "Owner.h" @interface Question : NSObject <NSCoding> {

Swift: Does not conform to protocol NSCoding

喜夏-厌秋 提交于 2019-12-18 04:45:22
问题 I am trying to use the NSCoding protocol on a class I have written in swift, but cannot seem to figure out why the compiler complains that it "does not conform to protocol NSCoding" when I do implement the required methods: class ServerInfo: NSObject, NSCoding { var username = "" var password = "" var domain = "" var location = "" var serverFQDN = "" var serverID = "" override init() { } init(coder aDecoder: NSCoder!) { self.username = aDecoder.decodeObjectForKey("username") as NSString self

Xcode 9 GM - WKWebView NSCoding support was broken in previous versions

喜夏-厌秋 提交于 2019-12-17 05:45:11
问题 Does anyone know how to fix this error with Xcode 9 GM? I'm working on an app made with Xcode 8.3, the deployment target is for iOS 9.3 and I never had this problem before. I don't find any information here or on Apple forums yet :( Edit: This error came when I put a WKWebView into interface builder, not if I use it programmatically. Edit 2: Well, it's finally not a bug, see Quinn's answer below to have more information about this behavior. Thanks to him for the explanation. 回答1: The error is

create a class using NSCoding

痴心易碎 提交于 2019-12-13 08:38:02
问题 in my first project I was creating a class using the following code: import Foundation class Rate { var currency: String! var sellRate: String! var buyRate: String! init (data: NSDictionary) { self.currency = getStringFromJSON(data, key:"CurrencyName") self.sellRate = getStringFromJSON(data, key:"SellRate") self.buyRate = getStringFromJSON(data, key:"BuyRate") } func getStringFromJSON(data: NSDictionary, key: String) -> String { if let info = data[key] as? String { return info } return "" } }

NSKeyedArchiver returning unexpected class?

依然范特西╮ 提交于 2019-12-12 12:10:05
问题 I have a custom class that extends NSString . I'm attempting to serialize it (for drag/drop) using an NSKeyedArchiver . The class overrides the ...Coder methods: - (id)initWithCoder:(NSCoder *)aDecoder { if ((self = [super initWithCoder:aDecoder])) { data = [[aDecoder decodeObjectForKey:@"data"] copy]; } return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [super encodeWithCoder:aCoder]; [aCoder encodeObject:self.data forKey:@"data"]; } But when I try to actually run through the

How to save a generic custom object to UserDefaults?

孤街浪徒 提交于 2019-12-12 09:52:59
问题 This is my generic class: open class SMState<T: Hashable>: NSObject, NSCoding { open var value: T open var didEnter: ( (_ state: SMState<T>) -> Void)? open var didExit: ( (_ state: SMState<T>) -> Void)? public init(_ value: T) { self.value = value } convenience required public init(coder decoder: NSCoder) { let value = decoder.decodeObject(forKey: "value") as! T self.init(value) } public func encode(with aCoder: NSCoder) { aCoder.encode(value, forKey: "value") } } Then I want to do this: let

Retrieve array function not working

橙三吉。 提交于 2019-12-12 05:27:04
问题 I created two functions called save array and retrieve array and their job is to save and retrieve an array from the phone. My problem is that they are not working. These are the two functions. func SaveArray (array: [IOU],fileID: String){ NSKeyedArchiver.archiveRootObject(array, toFile: fileID) } func RetrieveArray (fileID: String, var array: [IOU]) -> [IOU]{ if let arraytoRetrieve = NSKeyedUnarchiver.unarchiveObjectWithFile(fileID) as? [IOU]{ array = arraytoRetrieve } return array } IOU is

How do I decode a Double using NSCoder, NSObject and Swift 3 for iOS

痴心易碎 提交于 2019-12-12 02:25:36
问题 I am trying to store and load an object from persistent storage in Xcode 8.0 using Swift. I have followed the Start Developing iOS Apps (Swift): Jump Right In tutorial from Apple and had the same problem with the integer value for the star-rating. This is a "cropped" version of my class 'Expense' to show the 'amount' variable which I'm having trouble with: class Expense: NSObject, NSCoding { var amount: Double static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: