如何将以下ObjC代码的最后一行转换为
Swift:
CGAffineTransform transform = CGAffineTransformIdentity; CIFilter *clampFilter = [CIFilter filterWithName:@"CIAffineClamp"]; [clampFilter setValue:[NSValue valueWithBytes:&transform objCType:@encode(CGAffineTransform)] forKey:@"inputTransform"];
以下行
clampFilter.setValue(transform,forKey: "inputTransform")
产生
Type 'CGAffineTransform' does not conform to protocol 'AnyObject'
解决方法
NSValue有一个构造函数,它接受一个CGAffineTransform参数:
let transform = CGAffineTransformIdentity let clampFilter = CIFilter(name: "CIAffineClamp") let val = NSValue(CGAffineTransform: transform) clampFilter.setValue(val,forKey: "inputTransform")
Swift 3更新:
let transform = CGAffineTransform.identity let clampFilter = CIFilter(name: "CIAffineClamp")! let val = NSValue(cgAffineTransform: transform) clampFilter.setValue(val,forKey: "inputTransform")