这应该是微不足道的...我想,但我找不到如何将一个Struct变量包装成一个NSObject.有没有办法这样做?如果没有,我将如何添加一个结构NSMutableArray?
谢谢.
正如我的问题标题说我怎么能转换CGPoint成NSValues所以我可以存储然后进入数组在swift中.
在Objective-C中,我们可以这样做:
  // CGPoint converted to NSValue
     CGPoint point = CGPointMake(9, 9);
     NSValue *pointObj = [NSValue valueWithCGPoint:point];
但有人能告诉我,我怎么能在swift中做到这一点?
提前致谢.
在CABasicAnimation.fromValue我想要转换CGPoint为"类"所以我使用NSValue valueWithPoint但在设备模式或模拟器一个不工作...需要使用NSMakePoint或CGPointMake如果在设备或模拟器.
我通过对象和以下代码行存储Chipmunk Physics的cpShape对象:NSMutableDictionaryNSValue
NSValue *shapeValue = [[NSValue alloc] initWithBytes: shape objCType: @encode(cpShape)];
[staticBodiesInUse setObject: shapeValue forKey: name];
我现在需要cpShape退出,将它与另一种形状进行比较.我怎样才能做到这一点?我看到一个getValue:方法,NSValue但它需要一个缓冲区,不太清楚如何处理它.
我试图对一个MKMapView中心进行编码并跨越一个NSKeyedArchiver状态保存.我找到了几个方便的新MapKit NSValue添加,valueWithMKCoordinate:和valueWithMKCoordinate:.尝试将这些编码到密钥存档器中失败:
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
    NSValue *mapCenterValue = [NSValue valueWithMKCoordinate:mapView.centerCoordinate];
    NSValue *mapSpanValue = [NSValue valueWithMKCoordinateSpan:mapView.region.span];
    [coder encodeObject:mapCenterValue forKey:kMapCenter];
    [coder encodeObject:mapSpanValue forKey:kMapSpan];
}
由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [NSKeyedArchiver encodeValueOfObjCType:at:]:此归档程序无法对结构进行编码
我知道这个问题的解决方案是将单个双打编码为四个单独的密钥.
我的问题是为什么会发生这种情况.一个NSValue是一个对象,为什么它告诉我"这个归档器不能编码结构"
我需要设置zoomScale回到1上一个UIScrollView,但我需要它来进行动画.我以为我可以使用CABasicAnimation这一点,但NSValue似乎并不具有"valueForFloat"或"valueForNSNumber",所以我不知道用什么animation.fromValue和animation.toValue为CABasicAnimation.有没有更好的方法来解决这个问题?
编辑:我需要添加什么才能工作?
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"zoomScale"];
        animation.duration = 2.3;
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];        
        animation.fromValue = [NSNumber numberWithFloat:myScrollview.zoomScale];
        animation.toValue = [NSNumber numberWithFloat:1.0];
        [myScrollview.layer addAnimation:animation forKey:@"zoomScale"];
这是我到目前为止所尝试的内容
func onKeyboardRaise(notification: NSNotification) {
    var notificationData = notification.userInfo
    var duration = notificationData[UIKeyboardAnimationDurationUserInfoKey] as NSNumber
    var frame = notificationData[UIKeyboardFrameBeginUserInfoKey]! as NSValue
    var frameValue :UnsafePointer<(CGRect)> = nil;
    frame.getValue(frameValue)
}
但我似乎总是崩溃frame.getValue(frameValue).
它有点令人困惑,因为文档UIKeyboardFrameBeginUserInfoKey说它返回一个CGRect对象,但是当我登录frame控制台时,它表示类似的东西NSRect {{x, y}, {w, h}}.
我正在尝试CLLocation在Core Data中保存数据.我的属性设置为可转换对象.
一些示例代码表明可以使用此objc转换为a来保存位置数据NSValue.
CLLocationCoordinate2D myLocation;
NSValue *locationValue = [NSValue valueWithBytes:&myLocation objCType:@encode(CLLocationCoordinate2D)]
// then put locationValue into storage - maybe an ObjC collection class or core data etc.
从数据存储中检索对象应该可以使用此范例.
CLLocationCoordinate2D myLocation;
NSValue *locationValue = // restored from your database or retrieved from your collection class
[locationValue getValue:&myLocation];
我试图推断这个想法并将整个CLLocation对象捕获到NSValue存储中CoreData,目标CLLocation是稍后从商店中恢复对象.
在测试各种方法时,我发现在尝试CLLocation从Core Data Store中解包对象时返回了nil .我CLLocation使用以下内容将对象放入商店:
//verified that I have a good CLLocation here
newManagedObject.setValue(location as CLLocation, forKey: "location") …有人可以请给我一个动画图像视图的不透明度的示例吗?
我甚至无法在目标c中找到一个好的例子
func showCorrectImage(element: AnyObject){
    var animation : CABasicAnimation = CABasicAnimation(keyPath: "opacity");
    animation.delegate = self
    animation.fromValue = NSValue(nonretainedObject: 0.0)
    animation.toValue = NSValue(nonretainedObject: 1.0)
    animation.duration = 1.0
    element.layer?.addAnimation(animation, forKey: nil)
}
我认为我有大部分权利(尽管不完全确定),有人可以帮助我吗?
element =图像视图
提前谢谢!〜
我试图利用AVPlayer年代addBoundaryTimeObserverForTimes从快捷方法和遇到一个问题转换CMTimes到NSValues,这是addBoundaryTimeObserverForTimes作为输入.最后我使用了一个C函数包装[NSString valueWithCMTime:]并从swift调用它,但有没有办法在纯swift中执行它?
nsvalue ×10
swift ×5
ios ×4
objective-c ×3
cgpoint ×2
iphone ×2
avfoundation ×1
avplayer ×1
chipmunk ×1
cllocation ×1
cmtime ×1
cocoa ×1
cocoa-touch ×1
core-data ×1
mapkit ×1
struct ×1
uiimageview ×1
uiscrollview ×1