有没有办法保留 Swift 对 Decodable 类的默认实现,其中只有 Decodable 对象但有一个例外?例如,如果我有一个这样的结构/类:
struct MyDecodable: Decodable {
var int: Int
var string: String
var location: CLLocation
}
Run Code Online (Sandbox Code Playgroud)
我想使用默认解码int,string但location自己解码。所以在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) 我创建了一个名为 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 中的 …
我正在使用 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)