所以我写了一个简单的协议: protocol PopupMessageType{ var cancelButton: UIButton {get set} func cancel() } 并有一个customView: class XYZMessageView: UIView, PopupMessageType { … } 然后我现在有: class PopUpViewController: UIViewController { //code… var messageView : CCPopupMessageView! private func setupUI(){ view.addSubview(messageView) } } 但是我想要做的是: class PopUpViewController: UIViewController { //code… var messageView : PopupMessageType! private func setupUI(){ view.addSubview(messageView) // ERROR } } 错误我得到: 无法转换types“PopupMessageType!”的值 […]
我试图实现内置的iOS 5脸部检测API。 我正在使用UIImagePickerController的实例来允许用户拍照,然后我试图使用CIDetector来检测面部特征。 不幸的是, featuresInImage总是返回一个大小为0的数组。 代码如下: – (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage* picture = [info objectForKey:UIImagePickerControllerOriginalImage]; NSNumber *orientation = [NSNumber numberWithInt: [picture imageOrientation]]; NSDictionary *imageOptions = [NSDictionary dictionaryWithObject:orientation forKey:CIDetectorImageOrientation]; CIImage *ciimage = [CIImage imageWithCGImage:[picture CGImage] options:imageOptions]; NSDictionary *detectorOptions = [NSDictionary dictionaryWithObject:CIDetectorAccuracyLow forKey:CIDetectorAccuracy]; CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:detectorOptions]; NSArray *features = [detector featuresInImage:ciimage]; […]
假设我有以下协议: protocol Identifiable { var id: Int {get} var name: String {get} } 而且我有以下结构: struct A: Identifiable { var id: Int var name: String } struct B: Identifiable { var id: Int var name: String } 正如你所看到的,我必须“符合”在结构A和结构B的可识别协议。但想象一下,如果我有更多的结构需要符合这个协议…我不想'复制/粘贴一致性(var id:Int,var name:String) 所以我创build了一个协议扩展 : extension Identifiable { var id: Int { return 0 } var name: String { […]