小编iVe*_*tis的帖子

Swift 5 默认可解码实现,只有一个例外

有没有办法保留 Swift 对 Decodable 类的默认实现,其中只有 Decodable 对象但有一个例外?例如,如果我有一个这样的结构/类:

struct MyDecodable: Decodable {
   var int: Int
   var string: String
   var location: CLLocation
}
Run Code Online (Sandbox Code Playgroud)

我想使用默认解码intstringlocation自己解码。所以在init(from decoder:)我想有这样的事情:

required init(from decoder: Decoder) throws {
    <# insert something that decodes all standard decodable properties #>

    // only handle location separately
    let container = try decoder.container(keyedBy: CodingKeys.self)
    location = <# insert custom location decoding #>
}
Run Code Online (Sandbox Code Playgroud)

ios swift codable decodable swift5

6
推荐指数
1
解决办法
376
查看次数

单例框架 sharedInstance 可从 iOS 和 WatchKit Target 访问

我创建了一个名为 SharedLocation 的 Swift 框架,其中包含一个 Swift 单例类“SharedLocationManager”,如下所示:

public class SharedLocationManager: CLLocationManager, CLLocationManagerDelegate
{
public class var sharedInstance: SharedLocationManager {
    struct Static
    {
        static var onceToken : dispatch_once_t = 0
        static var instance : SharedLocationManager? = nil
    }
    dispatch_once(&Static.onceToken)
    {
            Static.instance = SharedLocationManager()
    }
    return Static.instance!
}

public override init()
{
    //do init stuff

}
Run Code Online (Sandbox Code Playgroud)

此类的共享实例应该可以从我的 iOS 应用程序(用 Objective-C 编写)和我的 WatchKit 扩展(用 Swift 编写)访问。

我在 iOS ViewController 中导入了框架,如下所示:

 @import SharedLocation
Run Code Online (Sandbox Code Playgroud)

在 Watch InterfaceController 中,如下所示:

import SharedLocation
Run Code Online (Sandbox Code Playgroud)

我能够在两个目标中获得单例类的实例,但这些是不同的实例(init() 被调用两次)。当我访问 WatchKit Target 中的 …

singleton instance targets ios watchkit

5
推荐指数
1
解决办法
1695
查看次数

iOS 9 UIImagePickerController 视频模式模糊

我正在使用 anUIImagePickerController来捕获视频并存储它。当我尝试使用静态图像捕获时一切正常,但是当我UIImagePickerController在视频模式下打开时,图片总是有点模糊。当我尝试聚焦不同的对象时,我看到焦点发生了变化,但它仍然有点模糊(因此从未真正聚焦)。这是一个已知问题iOS 9吗?我还尝试了一些来自互联网的示例项目,但结果相同。我用我的iPhone 6s和一个iPhone 6.

这是我使用的代码:

    func actionSheet(sheet: ActionSheet, didSelectOption option: Int) {

    let picker = UIImagePickerController()
    picker.delegate = self
    picker.navigationBar.tintColor = UIColor.whiteColor()

    let statusView = UIView(frame: UIApplication.sharedApplication().statusBarFrame)
    statusView.y = (-(UIApplication.sharedApplication().statusBarFrame.height))
    statusView.backgroundColor = colorBlue
    picker.navigationBar.insertSubview(statusView, atIndex: 1)


    if sheet.tag == actionSheetTagTakePicture {
        picker.allowsEditing = true
        picker.sourceType = option == 1 ? .Camera : .PhotoLibrary
        if option == 1 {
            picker.cameraDevice = .Front
        }
    }
    else {
        picker.mediaTypes = [kUTTypeMovie as …
Run Code Online (Sandbox Code Playgroud)

video camera uiimagepickercontroller ios ios9

0
推荐指数
1
解决办法
701
查看次数