【问题标题】:How to force asynchronously save a constant subclass?如何强制异步保存常量子类?
【发布时间】:2016-03-16 12:59:39
【问题描述】:

编辑 1:我已经重组了我的 ViewController 以便更轻松地完成我想要完成的工作。

编辑 2:我意识到在我的代码中添加注释时缺少一些重要的东西,另一个函数覆盖了第一个 segue。

这个 ViewController 是创建注解的地方;从这个视图中我需要的只是将 touchMapCoordinates 传输到另一个 ViewController,这样我就可以将 PFGeoPoint 保存在一个数组中。

编辑 3 经过长时间的了解正在发生的事情并简化代码后,我得出了基于 Swift- variable not initialized before use (but it's not used) 的最终结论,即我尝试使用的当前方法在任何情况下都不起作用或由于场景对其进行异步保存。如果有人知道解决方法,那么您已经正式完成了以前没有做过的事情:)。

出现的错误是

在初始化之前使用常量“boi”

在 Appdata 中声明的子类可在项目中的任何位置使用

import Foundation
import Parse
import MapKit

class MyAnnotation: PFObject, PFSubclassing, MKAnnotation {

// MARK: - Properties

@NSManaged var location: PFGeoPoint

// MARK: - Initializers

init(coordinate: CLLocationCoordinate2D) {
    super.init()
    self.location = PFGeoPoint(latitude: coordinate.latitude, longitude: coordinate.longitude)
    print(location)
    
}

override class func initialize() {
    struct Static {
        static var onceToken : dispatch_once_t = 0;
    }
    dispatch_once(&Static.onceToken) {
        self.registerSubclass()
        
        
    }
}

// MARK: - PFSubclassing protocol

static func parseClassName() -> String {
    return "AnnotationPins"
}

// MARK: - MKAnnotation protocol

var coordinate: CLLocationCoordinate2D {
    return CLLocationCoordinate2DMake(location.latitude, location.longitude)
}

var 标题:字符串? = "开始主题"

}

代码将全部异步保存在哪里

   } else {
        
        let imageData = UIImagePNGRepresentation(self.galleryCameraImage.image!)
        let parseImageFile = PFFile(name: "upload_image.png", data: imageData!)
        let boi : MyAnnotation
        
        let textTitleandText = PFObject(className: "AnnotationPins")
        textTitleandText["textTopic"] = userTopic.text
        textTitleandText["textInformation"] = userText.text
        textTitleandText["userUploader"] = PFUser.currentUser()
        textTitleandText["imageFile"] = parseImageFile!
        textTitleandText["location"] = boi.location
        
        textTitleandText.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
            if error == nil {

如果有人能提供帮助,将不胜感激!

【问题讨论】:

    标签: ios swift parse-platform mkannotation pfsubclassing


    【解决方案1】:

    覆盖prepareForSegue 方法,如下所示。

    override func prepareForSegue(segue: UIStoryboardSegue, sender: 
    
    AnyObject?) {
           if segue.identifier == "SegueID" {
               let destinationVC = segue.destinationViewController as! DestinationViewController  
    // Create property in destinationView controller & assign required data from here.
           }
        }
    

    希望对你有帮助。

    【讨论】:

    • 我已经更新了我的代码以准确地模拟我目前的情况。
    【解决方案2】:

    让我们将您的Location data 视为要通过segue 传输的普通数据。 您可以使用此方法配置将保存您的位置数据的目标 View 控制器变量(相同类型)。

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
       //check your segue name
       if segue.identifier == "YourSegueIdentifier" {
           let destinationVC = segue.destinationViewController as! YourDestinationViewController
           destinationVC.locationVariableInDestinationVC = locationVariableInCurrentVC 
       }
    
    }
    

    以上是通过 segue 传递数据的最简单方法,您也可以对位置数据使用相同的方法。

    希望有帮助!!

    Update: Based on your updated code

    func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {} 移出handleLongPress 函数。 PrepareForSegue 函数会在通过 segues 发生任何导航时自动调用..

    如果您想以编程方式启动 segue 导航,则为 segue 分配一个标识符,然后调用 self.performSegueWithIdentifier("YourSegueIdentifier", sender: nil)

    【讨论】:

    • } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "China" { let destinationVC = segue.destinationViewController as! AnnotationViewController destinationVC.location = MyAnnotation } } 所以在代码中我不断收到错误类型'ViewController'没有成员位置
    • 你必须在你的目标视图控制器中添加位置变量来保存你的数据。就像您正在为目标视图控制器中可用的变量赋值一样。希望有意义:)
    • ` var locationdata = PFGeoPoint() ` 所以我在目标视图控制器中有它,但是当我将它设置为它时它仍然无法工作 = 地图视图控制器中的位置数据
    • 您的myAnnotation 变量是否属于PFGeoPoint 类型?正如您已声明 locationdata 因此它将是 destinationVC.locationdata = MyAnnotation
    • 我已经更新了代码,更新了我一直试图解决的整体问题,myAnnotation 是 PFGeoPoint 的一个变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    相关资源
    最近更新 更多