简体   繁体   中英

Save Attributed Text to Parse in Swift

I am creating a basic note taking though parse using Swift. I want to save the attributed text to parse, but I can't figure out how. I've look online and I was not successful. Does anyone have any ideas? Here is my code:

NotesTableViewController.swift:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! NotesTableViewCell

let object: PFObject = self.notObjects.objectAtIndex(indexPath.row) as! PFObject

cell.masterCell?.text = object["title"] as? String
cell.masterTextLabel?.text = object["text"] as? String

return cell

addNoteTableController.swift:

override func viewDidLoad() {
super.viewDidLoad()

self.colorPicker.alpha = 0
self.fontPicker.alpha = 0

if (self.object != nil) {
    self.titleField?.text = self.object["title"] as? String
    self.textView?.text = self.object["text"] as? String
}else {
    self.object = PFObject(className: "Note")
    }
}

@IBAction func saveAction(sender: UIBarButtonItem) {
self.object["username"] = PFUser.currentUser()?.username
self.object["title"] = self.titleField?.text
self.object["text"] = self.textView?.text

self.object.saveEventually { (sucess, error) -> Void in
    if (error == nil) {
    }else {
        print(error?.userInfo)
    }
}

self.navigationController?.popViewControllerAnimated(true)
}

Note: Please be very specific in your answers. Don't just give me a link. I really need the code written for me at this point.... THANKS!!!!!

You have an NSAttributedString , which supports coding, so you can use an NSKeyedArchiver to create an NSData instance representing the raw attributed string. This includes the text and all of the formatting.

Using this data you can create a PFFile and save it, you also need to associate this file with another object in your parse store so you can get it back later. When you do you can get the NSData from it and use an NSKeyedUnarchiver to unpack it back into an NSAttributedString .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM