kvc

Why can I not use KVC from an Objective-C object to a Swift Property?

不羁岁月 提交于 2019-12-05 04:59:33
My team has decided that new files should be written in swift, and I am seeing an odd problem with using KVC in an Objective-C object to set a property on a Swift object. My Objective-C sets a property like so: [textObject setValue:0.0 forKey:@"fontSize"] My Swift object ( textObject ) has a custom setter/getter for this property. var fontSize: CGFloat? { get { return internalTextGraphic?.fontSize } set { internalTextGraphic?.fontSize = newValue } } However, if I set a breakpoint in the set , it never gets hit. I have Objective-C objects that also get this same call, and I just implement

How do I use Key-Value Coding in Swfit 4.0?

拜拜、爱过 提交于 2019-12-05 04:55:32
问题 I never used Swift4 before, and dont know how to use KVC in it. I try to create model with Dictionary, here the code: class Person : NSObject { var name: String = "" var age: Int = 0 init(dict: [String : Any]) { super.init() self.setValuesForKeys(dict) } } let dict: [String : Any] = ["name" : "Leon", "age" : 18] let p = Person(dict: dict) print(p.name, p.age) There I get two question: 1. Why dont I using AnyObject ? "Leon" and 18 was infer to String and Int , does it using in KVC? 2. @objc

How do I use Key-Value Coding in Swfit 4.0?

拥有回忆 提交于 2019-12-03 17:08:45
I never used Swift4 before, and dont know how to use KVC in it. I try to create model with Dictionary, here the code: class Person : NSObject { var name: String = "" var age: Int = 0 init(dict: [String : Any]) { super.init() self.setValuesForKeys(dict) } } let dict: [String : Any] = ["name" : "Leon", "age" : 18] let p = Person(dict: dict) print(p.name, p.age) There I get two question: 1. Why dont I using AnyObject ? "Leon" and 18 was infer to String and Int , does it using in KVC? 2. @objc var name: String = "" , this form is worked, but I can not understand it. Thanks for all helps. To

从OC到Swift(5)- 动态性,KVC\KVO

匿名 (未验证) 提交于 2019-12-03 00:08:02
dynamic 被@objc dynamic 修饰的内容会具有动态性,比如调用方法会走runtime那一套流程 class Dog : NSObject { @objc dynamic func test1 () {} func test2 () {} } var d = Dog () d . test1 () d . test2 () 对应汇编 KVC\KVO Swift支持KVC\KVO的条件 属性所在的类、监听器最终继承自NSObject 用@objc dynamic 修饰对应的属性 class Observer : NSObject { override func observeValue ( forKeyPath keyPath : String ?, of object : Any ?, change : [ NSKeyValueChangeKey : Any ]?, context : UnsafeMutableRawPointer ?) { print ( "observeValue" , change ?[. newKey ] as Any ) } } class Person : NSObject { @objc dynamic var age : Int = 0 var observer : Observer = Observer () override

iOS无埋点数据SDK实践之路

匿名 (未验证) 提交于 2019-12-02 23:03:14
SDK 已经具备不需要代码埋点就能 自动的、动态可配的、全面且正确 的收集用户在使用 App 时的所有事件数据。除此之外,还单独开发了与之配合的圈选SDK,能够在 App 端完成对界面元素的圈配以及 KVC 配置的上传。而界面元素圈配的工作完全可以交给用研与产品人员来做,减轻了开发人员的工作量。 SDK 已有的功能可以分为两大部分: 基本事件数据的收集:基本事件的收集是指应用冷启动事件、页面事件、用户点击事件、ScrollView滑动事件等,这部分全部都是自动完成的,实现思路会在第一节中介绍。 业务层数据的收集:业务层数据的收集是指对与业务功能相关的一些数据,例如:在用户点击提交订单按钮时,收集用户购买的物品以及订单总金额的数据。这种业务层数据的收集以往大多通过 代码埋点 的方式去做,本SDK则真正的实现了 无埋点 的去获取这些想要的业务数据。这部分的实现会在本文的第二节详细介绍。 SDK的整体实现思路 SDK 整体采用了 AOP(Aspect-Oriented-Programming)即面向切面编程的思想,就是动态的在函数调用的前后插入数据收集的代码。在 Objective-C 中的实现是基于 Runtime 特性的 Method Swizzling 黑魔法。 SDK 的数据收集功能的实现主要通过 Method Swizzling 来 hook 相应的方法

升级xcode11&ios13的坑

烂漫一生 提交于 2019-11-30 09:23:38
1.私有API被封禁(KVC限制),禁止访问. iOS13中通过KVC方式来获取私有属性,有Carsh风险,尽量避免使用.比如我们常用的UITextFiled和UISearchController等,在iOS 13的searchbar添加了一个- (void)set_cancelButtonText:(NSString *)text方法,这个方法专门用来命中kvc,一旦命中就Crash。 //修改textField的占位符字体颜色 [textField setValue:[UIColor xxx] forKeyPath:@"_placeholderLabel.textColor"]; (1).获取SearchBar的textField 由于在13中把SearchBar中的textField直接暴露给开发者使用,无需在通过kvc获取。 - (UITextField *)sa_GetSearchTextFiled{ if ([[[UIDevice currentDevice]systemVersion] floatValue] >= 13.0) { return self.searchTextField; }else{ UITextField *searchTextField = [self valueForKey:@"_searchField"]; return

从OC到Swift(5)- 动态性,KVC\KVO

倾然丶 夕夏残阳落幕 提交于 2019-11-30 00:42:11
dynamic 被@objc dynamic 修饰的内容会具有动态性,比如调用方法会走runtime那一套流程 class Dog: NSObject { @objc dynamic func test1() {} func test2() {} } var d = Dog() d.test1() d.test2() 对应汇编 KVC\KVO Swift支持KVC\KVO的条件 属性所在的类、监听器最终继承自NSObject 用@objc dynamic 修饰对应的属性 class Observer: NSObject { override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) { print("observeValue", change?[.newKey] as Any) } } class Person: NSObject { @objc dynamic var age: Int = 0 var observer: Observer = Observer() override init() { super.init() self

Why is NSUserDefaults unwilling to save my NSDictionary?

白昼怎懂夜的黑 提交于 2019-11-29 03:41:55
I'm using KVC to serialize an NSObject and attempt to save it to NSUserDefaults , which is giving me an Attempt to insert non-property value when I try to store my NSDictionary . Following are the properties of the object in question, MyClass : @interface MyClass : NSObject @property (copy,nonatomic) NSNumber* value1; @property (copy,nonatomic) NSNumber* value2; @property (copy,nonatomic) NSString* value3; @property (copy,nonatomic) NSString* value4; @property (copy,nonatomic) NSString* value5; @property (copy,nonatomic) NSString* value6; @property (copy,nonatomic) NSString* value7; @property

Why is NSUserDefaults unwilling to save my NSDictionary?

耗尽温柔 提交于 2019-11-27 17:40:49
问题 I'm using KVC to serialize an NSObject and attempt to save it to NSUserDefaults , which is giving me an Attempt to insert non-property value when I try to store my NSDictionary . Following are the properties of the object in question, MyClass : @interface MyClass : NSObject @property (copy,nonatomic) NSNumber* value1; @property (copy,nonatomic) NSNumber* value2; @property (copy,nonatomic) NSString* value3; @property (copy,nonatomic) NSString* value4; @property (copy,nonatomic) NSString*

Prefer property-accessor or KVC style for accessing Core Data properties

和自甴很熟 提交于 2019-11-27 08:31:06
问题 I have read the iOS-related chapters of Marcus S. Zarra's Core Data: Data Storage and Management for iOS, OS X, and iCloud (2nd edition) to great benefit. I am wondering about one aspect, though, where the book suggests a style that is different from my own. The book's examples access NSManagedObject s' attributes and relationships by utilizing KVC (e.g. [recipe valueForKey: @"name"] ). There are (only) two brief explanations of how one could go about defining property accessors (e.g. for use