简体   繁体   English

可可绑定,默认值中的字符串值绑定到文本字段。 如何以编程方式读取此字符串?

[英]Cocoa Bindings, String value in Defaults bound to a Text Field. How can I programmatically read this string?

I have a cocoa binding between UserDefaults and a TextField setup via IB: 我通过IB在UserDefaultsTextField设置之间有可可绑定:

在此处输入图片说明

This works great, on every launch of the app the TextField will persist it's value 效果很好,每次启动应用程序时, TextField都会保持其价值

However, I also want to read this string value programmatically in other places in code. 但是,我也想在代码的其他地方以编程方式读取此字符串值。 I have tried basically all of the UserDefaults.standard calls: 我基本上已经尝试了所有UserDefaults.standard调用:

guard let supplierData = UserDefaults.standard.data(forKey: "singleSupplierDownloadName") else {return}

returns: ▿ 145 bytes
  - count : 145   ▿ pointer : 0x0000608000187ae0
    - pointerValue : 106102873684704

UserDefaults.standard.string(forKey:) returns nil UserDefaults.standard.string(forKey:)返回nil

object(forKey:) returns this: object(forKey:)返回以下内容:

▿ Optional<Any>
  - some : <62706c69 73743030 d4010203 04050609 0a582476 65727369 6f6e5824 6f626a65 63747359 24617263 68697665 72542474 6f701200 0186a0a2 07085524 6e756c6c 57746573 74696e67 5f100f4e 534b6579 65644172 63686976 6572d10b 0c54726f 6f748001 08111a23 2d32373a 40485a5d 62000000 00000001 01000000 00000000 0d000000 00000000 00000000 00000000 64>

Given this, I decided to try to do something with the Data I retrieved with data(forKey:) 鉴于此,我决定尝试做的一些Data我检索data(forKey:)

(lldb) po String(data: supplierData, encoding: .ascii)
▿ Optional<String>
  - some : "bplist00Ô\u{01}\u{02}\u{03}\u{04}\u{05}\u{06}\t\nX$versionX$objectsY$archiverT$top\u{12}\0\u{01} ¢\u{07}\u{08}U$nullWtesting_\u{10}\u{0F}NSKeyedArchiverÑ\u{0B}\u{0C}Troot\u{01}\u{08}\u{11}\u{1A}#-27:@HZ]b\0\0\0\0\0\0\u{01}\u{01}\0\0\0\0\0\0\0\r\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0d"

I played around with the different encoders, and it was all junk. 我玩弄了不同的编码器,结果全都是垃圾。 Until I used ascii, you can see that there is hints of English in there, "NSKeyedArchiver", "unarchiver", etc. Made me believe that this data is actually not just a String, but a compressed String (archived) 在使用ascii之前,您可以看到那里有英语的提示,例如“ NSKeyedArchiver”,“ unarchiver”等。让我相信此数据实际上不仅是String,而且是压缩的String(存档)

so then I thought maybe I need to initialize an NSKeyedUnarchiver , 所以我想也许我需要初始化一个NSKeyedUnarchiver

(lldb) po NSKeyedUnarchiver(forReadingWith: supplierData)
<NSKeyedUnarchiver: 0x60800010beb0>

But from there... I don't know what to do. 但是从那里...我不知道该怎么办。 Everything I ask from the NSKeyedUnarchiver returns nil 我从NSKeyedUnarchiver询问的所有内容NSKeyedUnarchiver返回nil

Am I on the right track? 我在正确的轨道上吗?

How can I read this string value stored in the Shared User Defaults Controller via Cocoa Bindings? 如何通过Cocoa绑定读取存储在“共享用户默认值”控制器中的此字符串值?

The fix was not to use NSKeyedUnarchiveFromData as the value transformer on the binding (or any value transformer, at all). 解决的办法是不使用NSKeyedUnarchiveFromData作为绑定上的值转换器(或任何值转换器)。

When you use a value transformer, that makes the binding system transform the value from the view into some other value for the model to store and, when it retrieves a value from the model, to transform it to a value that the view can work with. 使用值转换器时,绑定系统使绑定系统将视图中的值转换为其他值供模型存储,并在从模型中检索值时将其转换为视图可以使用的值。 In your case, a text field works naturally with string values and so does the user defaults system, so there's no need or benefit to transforming to/from data objects. 在您的情况下,文本字段自然可以与字符串值一起使用,因此用户默认系统也是如此,因此在数据对象之间来回转换既没有必要也没有好处。

The main use for transforming via NSKeyedUnarchiveFromData is for values of types that can't be directly stored in property lists, such as color objects. 通过NSKeyedUnarchiveFromData进行转换的主要用途是用于不能直接存储在属性列表中的类型的值,例如颜色对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM