- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 MVC 应用程序。该模型有一个属性,它是一个结构 NSSize
。它可以这样写:
- (void)setSize:(NSSize)aSize;
View 使用键值编码设置此 NSSize
。但是,您不能对结构进行键值编码,所以我将它包装在 NSValue
对象中,如下所示:
[theView setValue:[NSValue valueWithSize:mySize]
forKey:@"theModel.size"];
根据我的理解,这应该不起作用,因为访问器需要一个结构而不是 NSValue
。但它工作得很好。神奇地。
这怎么可能?
最佳答案
向下滚动到“Wrapping and Unwrapping Structs”,但这里是要点:
setValue:forKey:
determines the data type required by the appropriate accessor or instance variable for the specified key. If the data type is not an object, then the value is extracted from the passed object using the appropriate-<type>Value
method.
顺便说一句,感谢您提出这个问题!这是 cocoa 一直“有效”的东西之一,我从没想过它是如何完成的。
关于objective-c - NSValue 是如何发挥它的魔力的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2495484/
如何创建包含 CGAffineTransform 的 NSValue 实例? UIKit 提供了[NSValue valueWithCGAffineTransform:],但 AppKit 没有。 我
所以我在这里读到,我可以使用将 CGLayerRef 编码为 NSValue NSValue *myCopy = [[NSValue alloc] initWithBytes:&myLayer obj
如何将非对象 Objective-C 不透明数据类型(如 CGImageRef、CGPDFPageRef)不透明类型转换为 NSValue 对象? 最佳答案 Check out this refere
我正在尝试创建一个跨平台的 NSValue 类别,它将处理 Cocoa 和 iOS 的 CGPoint/NSPoint 和 CGSize/NSSize 等。 我有这个: #ifdef __MAC_OS
我正在尝试将多维结构数组编码到 NSValue 中。 这是结构: typedef struct { float Position[3]; float Color[4]; } Verte
我在 XCode 中收到警告说 NSValue 可能不会响应 +valueWithCMTime:' 但我不确定我做错了什么。在我的头文件中,我有这个: #import #import ... -(
在我需要将所有数据成员保存在 NSDictionary 中的情况下,放置结构(自定义类型,甚至是标量,即 CGPoint)是否更有意义在我自己的包装器中(不是 NSValue),这样我就可以避免每次获
如何将下面的动画翻译成Swift3? CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"trans
我有一个 MVC 应用程序。该模型有一个属性,它是一个结构 NSSize。它可以这样写: - (void)setSize:(NSSize)aSize; View 使用键值编码设置此 NSSize。但是
我通过 NSValue 对象和以下代码行将 Chipmunk Physics 的 cpShape 对象存储在 NSMutableDictionary 中: NSValue *shapeValue =
我正在尝试使用 Facebook pop POPSpringAnimation 为 UIButton 的边界设置动画,但我找不到我应该在 swift 中使用什么来替换 NSValue.valueWit
我有一个 plist(在 XCode 中创建),其中包含一个充满“数字”(0.01、1、2、6)的数组,当使用 initWithContentsOfFile 重新构建时,该数组将解包到 NSValue
有什么方法可以使用 NSKeyedArchiver 来编码包含自定义结构的 NSValue 吗?我有一个 NSDictionary,其中包含封装在 NSValues 中的结构,我想将其存档为更大的对象
一些背景... 我正在编写通过 ObjC-JS 桥使用 UIWebView 与 javascript 交互的代码的stringByEvaluatingJavaScriptFromString: .这个
我正在尝试更改我的 UISegmentedControl 的文本阴影位置.为此,documentation说我需要通过 NSValue它包装了一个 UIOffset 结构。 UITextAttribu
我有一些代码,我将在其中接收未知类型的对象。它可以是 NSString、NSNumber、包裹在 NSValue 或其他类中的标量: -(void) doSomethingWith:(id) valu
如何将NSValue转换为CLLocationCoordinate2D? 我在 (NSArray *)coordinateArray 中收到 NSValue,并且需要为数组中的每个 NSValue 获
我需要将结构 AudioStreamBasicDescription 传递给随机 View Controller 。为此,我创建了一个单例类,我想将 AudioStreamBasicDescripti
我想将 NSValue 转换为 NSNumber。这段代码有什么问题? char value[4]; [myNSValue getValue:(void*)value]; NSNumber *numb
我只是想将 CLLocationCoordinates 转换为 NSValue 以用于稍后在我的应用程序中带回的数组。这是我的代码: NSUInteger count = [self.locati
我是一名优秀的程序员,十分优秀!