我正在使用一个具有四个日期属性的核心数据实体——syncDate、historySyncDate 等。
有没有一种方法可以编写一个方法来获取这些属性之一的 NSString 名称并为其分配适当的日期?
前任:
-(void)updateDate:(NSDate*) date forAttribute:(NSString*)attribute forService:(Service*)service
{
//based on attribute name, set service.syncDate or service.historicSyncDate, etc
}
Run Code Online (Sandbox Code Playgroud) I have seen people using UserDefaults.setValue()
occasionally. Below is an example I find here and it works fine.
@propertyWrapper struct AppSetting<Value> {
let key: String
let defaultValue: Value
var container: UserDefaults = .standard
var wrappedValue: Value {
get { container.value(forKey: key) as? Value ?? defaultValue}
set { container.setValue(newValue, forKey: key) }
}
}
Run Code Online (Sandbox Code Playgroud)
从我在Apple的文档中可以找到的是其KVO功能setValue()
的API ,并且类继承自. 但是,尚不清楚该 API 在被调用时具体执行什么操作。从上面的例子,我可以猜到(似乎覆盖了这个方法)。但否则我会认为它设置了对象的属性,这是没有意义的。所以我想知道人们如何找到这种用法?它在任何地方都有记录吗?NSObject
UserDefaults
NSObject
UserDefaults
UserDefaults
UserDefaults
更新:我可能没有清楚地描述我的问题。我知道 KVO 是什么。我感到困惑的是 KVO 与 UserDefaults 的关系。根据我的理解,KVO 中的 key 和 UserDefaults 中的 …
目标:使用优雅代码获取包含给定NSDictionary的唯一键的NSArray
带当前工作解决方案的示例代码
NSArray *data = [[NSArray alloc] initWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1], @"a", [NSNumber numberWithInt:2], @"b", nil],
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:3], @"b", [NSNumber numberWithInt:4], @"c", nil],
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:5], @"a", [NSNumber numberWithInt:6], @"c", nil],
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:7], @"b", [NSNumber numberWithInt:8], @"a", nil],
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:8], @"c", [NSNumber numberWithInt:9], @"b", nil],
nil];
// create an NSArray of all the dictionary keys within the NSArray *data
NSMutableSet *setKeys = [[NSMutableSet alloc] init];
for (int i=0; i<[data count]; i++) {
[setKeys addObjectsFromArray:[[data objectAtIndex:i] allKeys]]; …
Run Code Online (Sandbox Code Playgroud) 我有几个NSArray,它们包含相互关联的UIView对象集合(NSArrays是soundView0,soundView1,soundView2和soundView3).我希望能够将BOOL属性与将在数组中启用/解释UIViews的整个数组相关联.
什么是最干净/最恰当的方法来完成这个?
我有一个定义属性的 ClassA:
@interface ClassA : NSObject
@property (nonatomic) CGPoint property;
@end
Run Code Online (Sandbox Code Playgroud)
该实现不会覆盖访问器。
ClassB 覆盖 setter 来做一些额外的工作:
- (void)setProperty:(CGPoint)property {
[super setProperty:property];
[self someAdditionalWork];
}
Run Code Online (Sandbox Code Playgroud)
在 ClassB 的另一种方法中,我尝试通过超级设置器设置此属性,以跳过额外的工作:
- (void)otherMethodInClassB {
// ...
super.property = newValue;
// ...
}
Run Code Online (Sandbox Code Playgroud)
当我这样做时,不会发送该属性的 KVO 通知。如果我做同样的事情,但使用self
,KVO 通知按预期工作:
- (void)otherMethodInClassB {
// ...
self.property = newValue;
// ...
}
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?这是预期的行为吗?我找不到任何可以这样说的东西。