How to retrieve data from VCard using swift - xcode

I'm working on QR code scanning. Using the following swift code, the data in the QR code is getting printed in the console.
import UIKit
import AVFoundation
class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate
{
let session = AVCaptureSession()
#IBOutlet weak var square: UIImageView!
var video = AVCaptureVideoPreviewLayer()
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
if metadataObjects != nil && metadataObjects.count != 0
{
if let object = metadataObjects[0] as? AVMetadataMachineReadableCodeObject
{
if object.type == AVMetadataObjectTypeQRCode
{
print(object.stringValue)
let alert = UIAlertController(title: "QR Code", message: object.stringValue, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okey", style: .default, handler: nil))
/* alert.addAction(UIAlertAction(title: "Copy", style: .default, handler: { (nil) in
UIPasteboard.general.string = object.stringValue
}))
*/
present(alert, animated: true, completion: nil)
if metadataObjects.count > 0
{
print("-------------------------------------------------")
self.session.stopRunning()
}
}
}
}
}
#IBAction func startButton(_ sender: Any)
{
// Creating session
// let session = AVCaptureSession()
// Define capture device
let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
do {
let input = try AVCaptureDeviceInput(device: captureDevice)
session.addInput(input)
} catch {
print("Error")
}
let output = AVCaptureMetadataOutput()
session.addOutput(output)
output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
output.metadataObjectTypes = [AVMetadataObjectTypeQRCode]
video = AVCaptureVideoPreviewLayer(session: session)
video.frame = view.layer.bounds
view.layer.addSublayer(video)
self.view.bringSubview(toFront: square)
session.startRunning()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
OUTPUT:
BEGIN:VCARD
VERSION:2.1
FN:John Peter
N:Peter;John
TITLE:Admin
TEL;CELL:+91 431 524 2345
TEL;WORK;VOICE:+91 436 542 8374
EMAIL;WORK;INTERNET:John#ommail.in
URL:www.facebook.com
ADR;WORK:;;423 ofce sales Center;Newark;DE;3243;USA
ORG:xxx Private limited
END:VCARD
Now what I want is, how to retrieve the data in the VCard format specifically, like getting first name, last name, email id, mobile number, etc. A similar question to this is asked before, but it was in objective-c. I don't know how to do this swift 3. Thanks in advance.

Here's a sample code from Playground I used. It's not the best, but You'll get the idea:
import UIKit
import Contacts
var str = "BEGIN:VCARD \n" +
"VERSION:2.1 \n" +
"FN:John Peter \n" +
"N:Peter;John \n" +
"TITLE:Admin \n" +
"TEL;CELL:+91 431 524 2345 \n" +
"TEL;WORK;VOICE:+91 436 542 8374 \n" +
"EMAIL;WORK;INTERNET:John#ommail.in \n" +
"URL:www.facebook.com \n" +
"ADR;WORK:;;423 ofce sales Center;Newark;DE;3243;USA \n" +
"ORG:xxx Private limited \n" +
"END:VCARD"
if let data = str.data(using: .utf8) {
let contacts = try CNContactVCardSerialization.contacts(with: data)
let contact = contacts.first
print("\(String(describing: contact?.familyName))")
}
The output from CNContactVCardSerialization.contacts(with: data) is an array of CNContact (reference).

Here you can check Updated answer for swift 4 of Mateusz Szlosek
var str = "BEGIN:VCARD \n" +
"VERSION:2.1 \n" +
"FN:John Peter \n" +
"N:Peter;John \n" +
"TITLE:Admin \n" +
"TEL;CELL:+91 431 524 2345 \n" +
"TEL;WORK;VOICE:+91 436 542 8374 \n" +
"EMAIL;WORK;INTERNET:John#ommail.in \n" +
"URL:www.facebook.com \n" +
"ADR;WORK:;;423 ofce sales Center;Newark;DE;3243;USA \n" +
"ORG:xxx Private limited \n" +
"END:VCARD"
func fectchContact(){
if let data = str.data(using: .utf8) {
do{
let contacts = try CNContactVCardSerialization.contacts(with: data)
let contact = contacts.first
print("\(String(describing: contact?.familyName))")
}
catch{
// Error Handling
print(error.localizedDescription)
}
}
}

Related

applicationDidFinishLaunching not invoked on Console App

I'm trying to write a simple command line app that can display some info on a notification. But, the Delegate is not being called, and neither is the Notification and I'm not sure what's missing here.
Judging from my output, I think the whole problem stems from the AppDelegate not being instantiated. But I am creating one just before I show call showNotification.
What am I missing here?
src/main.swift
import Foundation
import AppKit
var sema = DispatchSemaphore( value: 0 )
let server: String = "http://jsonip.com"
let port: String = "80"
let path: String = "/"
let todoEndpoint: String = server + ":" + port + path
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let url = URL(string: todoEndpoint)!
let task = session.dataTask(with: url, completionHandler: {
(data, response, error) in
if error != nil {
print(error!.localizedDescription)
} else {
do {
if let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]
{
print(json)
let ad = AppDelegate()
ad.showNotification(title: "Title", subtitle: "SubTitle", informativeText: String(describing: json))
sema.signal()
}
} catch {
print("error in JSONSerialization")
}
}
})
print("Resume Task")
task.resume()
print("Wait for Semaphore")
sema.wait()
src/AppDelegate.swift
import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {
func applicationDidFinishLaunching(aNotification: Notification) {
NSUserNotificationCenter.default.delegate = self
print("Delegate Self")
}
// NSUserNotificationCenterDelegate implementation
private func userNotificationCenter(center: NSUserNotificationCenter!, didDeliverNotification notification: NSUserNotification!) {
//implementation
}
private func userNotificationCenter(center: NSUserNotificationCenter!, didActivateNotification notification: NSUserNotification!) {
//implementation
}
private func userNotificationCenter(center: NSUserNotificationCenter!, shouldPresentNotification notification: NSUserNotification!) -> Bool {
//implementation
return true
}
func showNotification(title: String, subtitle: String, informativeText: String) -> Void {
let notification: NSUserNotification = NSUserNotification()
print("Show Notification")
notification.title = title
notification.subtitle = subtitle
notification.informativeText = informativeText
//notification.contentImage = contentImage
notification.soundName = NSUserNotificationDefaultSoundName
NSUserNotificationCenter.default.deliver(notification)
print(notification.isPresented)
}
}
Output
Resume Task
Wait for Semaphore
["about": /about, "reject-fascism":
Impeach Trump!, "ip": 110.50.73.141, "Pro!": http://getjsonip.com]
Show Notification
false
Program ended with exit code: 0

Vision Framework Barcode detection for iOS 11

I've been implementing a test of the new Vision framework which Apple introduced in WWDC2017. I am specifically looking at the barcode detection - I've been able to get after scanning the image from Camera/Gallery that it's a barcode image or not. However, I can't see what the actual barcode value or the payload data when looking at the barcodeDescriptor. There appears to be nothing exposed on the https://developer.apple.com/documentation/coreimage/cibarcodedescriptor page to identify any of the properties.
I am getting these errors:
Cannot connect to remote service: Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named
com.apple.BarcodeSupport.BarcodeNotificationService"
libMobileGestalt MobileGestalt.c:555: no access to InverseDeviceID (see problem/11744455>)
connection to service named com.apple.BarcodeSupport.BarcodeNotificationService Error
Domain=NSCocoaErrorDomain Code=4097
Is there any way to access the barcode value from the VNBarcodeObservation?
Any help would be greatly appreciated. Thank you!
Here is the code I am using:
#IBAction func chooseImage(_ sender: Any) {
imagePicker.allowsEditing = true
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
#IBAction func takePicture(_ sender: Any) {
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)){
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
self .present(imagePicker, animated: true, completion: nil)
}
else{
let alert = UIAlertController(title: "Warning", message: "Camera not available", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
//PickerView Delegate Methods
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imagePicker .dismiss(animated: true, completion: nil)
classificationLabel.text = "Analyzing Image…"
guard let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
else { fatalError("no image from image picker") }
guard let ciImage = CIImage(image: pickedImage)
else { fatalError("can't create CIImage from UIImage") }
imageView.image = pickedImage
inputImage = ciImage
// Run the rectangle detector, which upon completion runs the ML classifier.
let handler = VNImageRequestHandler(ciImage: ciImage, options: [.properties : ""])
DispatchQueue.global(qos: .userInteractive).async {
do {
try handler.perform([self.barcodeRequest])
} catch {
print(error)
}
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
picker .dismiss(animated: true, completion: nil)
print("picker cancel.")
}
lazy var barcodeRequest: VNDetectBarcodesRequest = {
return VNDetectBarcodesRequest(completionHandler: self.handleBarcodes)
}()
func handleBarcodes(request: VNRequest, error: Error?) {
guard let observations = request.results as? [VNBarcodeObservation]
else { fatalError("unexpected result type from VNBarcodeRequest") }
guard observations.first != nil else {
DispatchQueue.main.async {
self.classificationLabel.text = "No Barcode detected."
}
return
}
// Loop through the found results
for result in request.results! {
// Cast the result to a barcode-observation
if let barcode = result as? VNBarcodeObservation {
// Print barcode-values
print("Symbology: \(barcode.symbology.rawValue)")
if let desc = barcode.barcodeDescriptor as? CIQRCodeDescriptor {
let content = String(data: desc.errorCorrectedPayload, encoding: .utf8)
// FIXME: This currently returns nil. I did not find any docs on how to encode the data properly so far.
print("Payload: \(String(describing: content))\n")
print("Error-Correction-Level: \(desc.errorCorrectedPayload)\n")
print("Symbol-Version: \(desc.symbolVersion)\n")
}
}
}
}
Apparently, in the iOS 11 beta 5 Apple introduced new payloadStringValue property of VNBarcodeObservation. Now you can read info from QR-code with no problems
if let payload = barcodeObservation.payloadStringValue {
print("payload is \(payload)")
}
If Apple is not going to provide a library for this, something like the following will work:
extension CIQRCodeDescriptor {
var bytes: Data? {
return errorCorrectedPayload.withUnsafeBytes { (pointer: UnsafePointer<UInt8>) in
var cursor = pointer
let representation = (cursor.pointee >> 4) & 0x0f
guard representation == 4 /* byte encoding */ else { return nil }
var count = (cursor.pointee << 4) & 0xf0
cursor = cursor.successor()
count |= (cursor.pointee >> 4) & 0x0f
var out = Data(count: Int(count))
guard count > 0 else { return out }
var prev = (cursor.pointee << 4) & 0xf0
for i in 2...errorCorrectedPayload.count {
if (i - 2) == count { break }
let cursor = pointer.advanced(by: Int(i))
let byte = cursor.pointee
let current = prev | ((byte >> 4) & 0x0f)
out[i - 2] = current
prev = (cursor.pointee << 4) & 0xf0
}
return out
}
}
}
And then
String(data: descriptor.bytes!, encoding: .utf8 /* or whatever */)
If you want to get the raw Data from the VNBarcodeObservation directly without it having to conform to some string encoding you can strip of the first 2 and 1/2 bytes like this, and get actual data without the QR code header.
guard let barcode = barcodeObservation.barcodeDescriptor as? CIQRCodeDescriptor else { return }
let errorCorrectedPayload = barcode.errorCorrectedPayload
let payloadData = Data(bytes: zip(errorCorrectedPayload.advanced(by: 2),
errorCorrectedPayload.advanced(by: 3)).map { (byte1, byte2) in
return byte1 << 4 | byte2 >> 4
})

Saved Parse Database into an array called recipes and am now trying to access content within that array

I have a database in parse that i have pulled into a swift array. The custom parse object is called UserRecipe. The array is called recipes and is located in the viewDidLoad method. I am trying to set the imageview i have called recipeImage, to always access the image of the first element in the array. I do this in the updateImage function but am not sure if I have the correct syntax. Also the array seems to be stored only with the viewDidLoad method and is not accessible to my updateImage function. I'm wondering how to make it global so all functions can access it. Thanks in advance for any help.
The database looks like this:
import UIKit
import Parse
//import ParseFacebookUtilsV4
import FBSDKCoreKit
import FBSDKLoginKit
class ViewController: UIViewController {
let recipes = [PFObject]?.self
#IBOutlet var recipeImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
//load in all data from Parse custom Object UserRecipe and store it in variable recipes
var query = PFQuery(className:"UserRecipe")
query.findObjectsInBackgroundWithBlock {
(recipes: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if let recipes = recipes {
for recipe in recipes {
print(recipe["recipeName"])
}
}
} else {
print("Error: \(error!) \(error!.userInfo)")
}
}
let gesture = UIPanGestureRecognizer(target: self, action: Selector("wasDragged:"))
recipeImage.addGestureRecognizer(gesture)
//let tapping = UITapGestureRecognizer(target: self, action: Selector("wasTapped:"))
//recipeImage.addGestureRecognizer(tapping)
recipeImage.userInteractionEnabled = true
//updateImage()
//getUserInfo()
}
func wasDragged(gesture: UIPanGestureRecognizer) {
//Dragging Animation
let translation = gesture.translationInView(self.view)
let imageDrag = gesture.view!
imageDrag.center = CGPoint(x: self.view.bounds.width / 2 + translation.x, y: self.view.bounds.height / 2 + translation.y - 153)
let xFromCenter = imageDrag.center.x - self.view.bounds.width / 2 + translation.x
let scale = min(100 / abs(xFromCenter), 1)
var rotation = CGAffineTransformMakeRotation(xFromCenter / 200)
var stretch = CGAffineTransformScale(rotation, scale, scale)
imageDrag.transform = stretch
//determines whether current user has accepted or rejected certain recipes
if gesture.state == UIGestureRecognizerState.Ended {
var acceptedOrRejected = ""
if imageDrag.center.x < 100 {
acceptedOrRejected = "rejected"
print("not chosen")
//print("not chosen" + object["recipeName"])
} else if imageDrag.center.x > self.view.bounds.width - 100 {
acceptedOrRejected = "accepted"
print("Chosen")
}
/*if acceptedOrRejected != "" {
PFUser.currentUser()?.addUniqueObjectsFromArray([displayedUserId], forKey: acceptedOrRejected)
PFUser.currentUser()?.saveInBackgroundWithBlock({
(succeeded: Bool, error: NSError?) -> Void in
if succeeded {
} else {
print(error)
}
})
}*/
//Resets image position after it has been let go of
rotation = CGAffineTransformMakeRotation(0)
stretch = CGAffineTransformScale(rotation, 1, 1)
imageDrag.transform = stretch
imageDrag.center = CGPoint(x: self.view.bounds.width / 2, y: self.view.bounds.height / 2 - 153)
updateImage()
}
}
func updateImage() {
recipeImage.image = recipes["image"][0]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I am not across Parse but if I were you these are things I would try,
Set a breakpoint in
func updateImage() {
// set breakpoint here and check whether recipes contains any data.
recipeImage.image = recipes["image"][0]
}
Replace the code as shown below,
//replace
recipes["image"][0]
//to
recipes[0]["image"]

querying an object with a pointer - parse, swift2

I have 2 classes, I have an Activity Class and JobPost class, in my Activity class there is an object called "postedJob" which is a pointer(JobPost), it stores the objectID of every job posted.
What I am trying to achieve is to query the postedJob from activity, but my code below form parse is throwing an error.
let jobPostObject = PFObject(className: "JobPost")
let query = PFQuery(className: "Activity")
query.whereKey("postedJob", equalTo: jobPostObject)
let objects = query.findObjects()
print(objects)
I dont understand why it's returning this error below
'Tried to save an object with a new, unsaved child.'
*** First throw call stack:
(
0 CoreFoundation 0x0000000108213f45 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000107c8bdeb objc_exception_throw + 48
2 CoreFoundation 0x0000000108213e7d +[NSException raise:format:] + 205
3 post 0x00000001063c5e92 -[PFPointerObjectEncoder encodeParseObject:] + 108
4 post 0x00000001063c5324 -[PFEncoder encodeObject:] + 113
5 post 0x00000001063948ef __129+[PFRESTQueryCommand findCommandParametersWithOrder:conditions:selectedKeys:includedKeys:limit:skip:extraOptions:tracingEnabled:]_block_invoke97 + 1808
6 CoreFoundation 0x0000000108180cb5 __65-[__NSDictionaryI enumerateKeysAndObjectsWithOptions:usingBlock:]_block_invoke + 85
7 CoreFoundation 0x0000000108180bbd -[__NSDictionaryI enumerateKeysAndObjectsWithOptions:usingBlock:] + 237
8 post 0x00000001063940fd +[PFRESTQueryCommand findCommandParametersWithOrder:conditions:selectedKeys:includedKeys:limit:skip:extraOptions:tracingEnabled:] + 911
9 post 0x0000000106393d2c +[PFRESTQueryCommand findCommandParametersForQueryState:] + 296
10 post 0x00000001063937df +[PFRESTQueryCommand findCommandForQueryState:withSessionToken:] + 79
11 post 0x00000001063a5739 __78-[PFQueryController findObjectsAsyncForQueryState:withCancellationToken:user:]_block_invoke + 106
12 post 0x000000010634a7be __37+[BFTask taskFromExecutor:withBlock:]_block_invoke + 78
13 post 0x000000010634bf20 __55-[BFTask continueWithExecutor:block:cancellationToken:]_block_invoke_2 + 112
14 libdispatch.dylib 0x000000010a2f1e5d _dispatch_call_block_and_release + 12
15 libdispatch.dylib 0x000000010a31249b _dispatch_client_callout + 8
16 libdispatch.dylib 0x000000010a2fabef _dispatch_root_queue_drain + 1829
17 libdispatch.dylib 0x000000010a2fa4c5 _dispatch_worker_thread3 + 111
18 libsystem_pthread.dylib 0x000000010a65a4f2 _pthread_wqthread + 1129
19 libsystem_pthread.dylib 0x000000010a658375 start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Edit 1 :
Import UIKit
class ActivityEmployerVC: UITableViewController {
var num = 0
var postJob: String?
var dataSource: [PFObject] = []
//MARK: - PULL TO REFRESH FUNCTION
func refresh(sender:AnyObject) {
// Updating your data here...
self.fetchDataFromParse()
self.refreshControl?.endRefreshing()
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.barTintColor = UIColor(netHex: 0x003366)
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
// Do any additional setup after loading the view.
self.refreshControl?.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
//self.refreshControl?.backgroundColor = UIColor(netHex: 0x003366)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(animated: Bool) {
fetchDataFromParse()
}
//MARK: - TABLE VIEW
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let query = PFQuery(className: "Activity")
let userPointer = PFUser.objectWithoutDataWithObjectId(PFUser.currentUser()?.objectId)
query.whereKey("fromUser", equalTo: userPointer)
let objects = query.findObjects()
self.num = (objects?.count)!
print(num)
return num
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cells"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier , forIndexPath: indexPath)
if dataSource.isEmpty{
fetchDataFromParse()
print("No Post")
} else {
if num == 0{
cell.textLabel?.text = "Start posting a job!"
} else {
//MARK: - TABLEVIEW TITLE CELL
let itemArr:PFObject = self.dataSource[indexPath.row]
postJob = itemArr["type"] as? String
if postJob == "jobPost"{
let jobPostObject = PFObject(className: "JobPost")
let query = PFQuery(className: "Activity")
query.whereKey("postedJob", equalTo: jobPostObject)
let objects = query.findObjects()
print(objects)
cell.textLabel?.text = "You posted a job - \(postJob!)"
}
//MARK: - TABLEVIEW DATE CELL
let createdDate = itemArr.createdAt
let form = NSDateComponentsFormatter()
form.maximumUnitCount = 2
form.unitsStyle = .Abbreviated // or .Full, whatever you prefer
let d = form.stringFromTimeInterval(NSDate.timeIntervalSinceReferenceDate() - createdDate!.timeIntervalSinceReferenceDate)
cell.detailTextLabel?.text = "\(d!) ago"
}//end of if num
}//end of datasource.isempty
return cell
}
//MARK: - DATA FETCHING FUNCTIONS
func fetchDataFromParse() {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in
//MARK: - CLEARING ARRAYS
self.dataSource.removeAll()
// MARK: - JOB POST QUERY
if PFUser.currentUser()?.objectId == nil{
PFUser.currentUser()?.saveInBackgroundWithBlock({ (success, error) -> Void in
let query = PFQuery(className: "Activity")
//creating a pointer
let userPointer = PFUser.objectWithoutDataWithObjectId(PFUser.currentUser()?.objectId)
query.whereKey("fromUser", equalTo: userPointer)
query.orderByDescending("createdAt")
let objects = query.findObjects()
for object in (objects as? [PFObject])!{
//print(object.objectId)
self.dataSource.append(object)
}//end of for loop
})//end of saveInBackground
} else {
let query = PFQuery(className: "Activity")
//creating a pointer
let userPointer = PFUser.objectWithoutDataWithObjectId(PFUser.currentUser()?.objectId)
query.whereKey("fromUser", equalTo: userPointer)
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock({ (objs, error) -> Void in
if error == nil {
for obj in (objs as? [PFObject])!{
//print(object.objectId)
self.dataSource.append(obj)
self.tableView.reloadData()
}//for loop
}//end of error == nil
})// end of findobjectsinbackground
print("query executed")
}//end of PFUser objectID == nil else clause
})
}
}
EDIT 2:
This is happening because jobPostObject is not a complete object, it's just an empty, unsaved JobPost.
The query itself doesn't make a lot of sense:
You first create a new JobPost
let jobPostObject = PFObject(className: "JobPost")
Then you are trying to find an Activity associated to this JobPost:
query.whereKey("postedJob", equalTo: jobPostObject)
Since jobPostObject is brand new, has no data, and more importantly, has never been saved to parse, how could this request return anything?
That's being said this error:
Tried to save an object with a new, unsaved child
means that you are trying to use an object that is dirty in a query. A dirty object is an object that is new OR is an existing object that has been modified, but that hasn't been saved in the database.
Try to add
query.includeKey("postedJob")
after all your
let query = PFQuery(className: "Activity")
then you can replace the jobPostObject part:
let jobPostObject = PFObject(className: "JobPost")
let query = PFQuery(className: "Activity")
query.whereKey("postedJob", equalTo: jobPostObject)
let objects = query.findObjects()
print(objects)
by something like
let jobPostObject = itemArr["postedJob"] as? PFObject

Populate tableview in swift

I have this result after parsing XML result.
When I do a println() I get the result perfectly like this
{
description = "Suzuki SX4 - BB71521";
deviceID = 359710042040320;
}
{
description = "Chevrolet Tahoe Noir - Demonstration";
deviceID = 359710042067463;
}
{
description = "Isuzu D'Max AA-08612";
deviceID = 359710042091273;
}
{
description = "Toyota 4 Runner";
deviceID = 359710042110768;
}
But when I try to parse the same result in an UITableView with two Cells, the UITableView appears empty.
Please help me to populate only the table ???
Here is my code
import UIKit
class mtTableViewController: UITableViewController, NSXMLParserDelegate {
var info = NSDictionary();
var parser = NSXMLParser()
var posts = NSMutableArray()
var elements = NSMutableDictionary()
var element = NSString()
override func viewDidLoad()
{
super.viewDidLoad()
self.beginParsing()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func beginParsing()
{
let url = NSURL(string: "http://localhost:8080")
var auth =
"<GTSRequest command=\"dbget\">" +
"<Authorization account=\"" + "easytrucking" + "\" user=\"" + "admin" + "\" password=\"" + "T8095634934ht" + "\"/>" +
"<Record table=\"Device\" partial=\"true\">" +
"<Field name=\"accountID\">" + "easytrucking" + "</Field>" +
"<Field name=\"description\"/>" +
"</Record>" +
"</GTSRequest>";
// "<Authorization account=\"" + txtAccount.text + "\" user=\"" + txtUserName.text + "\" password=\"" + txtPassword.text + "\"/>" +
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let data : NSData = (auth).dataUsingEncoding(NSUTF8StringEncoding)!;
request.HTTPBody = data;
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
/// println(NSString(data: data, encoding: NSUTF8StringEncoding))
let xml = SWXMLHash.parse(data)
let count = xml["GTSResponse"]["Record"].all.count
for var i = 0; i < count; i++
{
if (xml["GTSResponse"]["Record"][i]["Field"][1].element?.attributes["name"] == "deviceID")
{
self.elements.setObject((xml["GTSResponse"]["Record"][i]["Field"][1].element?.text)!, forKey: "deviceID")
}
if (xml["GTSResponse"]["Record"][i]["Field"][3].element?.attributes["name"] == "description")
{
self.elements.setObject((xml["GTSResponse"]["Record"][i]["Field"][3].element?.text)!, forKey: "description")
}
self.posts.addObject(self.elements)
// here i print the result
println(self.elements)
}
}
}
//Tableview Methods
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return posts.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell : UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
if(cell == nil) {
cell = NSBundle.mainBundle().loadNibNamed("Cell", owner: self, options: nil)[0] as UITableViewCell;
}
cell.textLabel?.text = posts.objectAtIndex(indexPath.row).valueForKey("deviceID") as NSString
cell.detailTextLabel?.text = posts.objectAtIndex(indexPath.row).valueForKey("description") as NSString
return cell as UITableViewCell
}
}
Please help !!!
There are two problems. First, NSURLConnection.sendAsynchronousRequest()
– as the name indicates – works asynchronously: It initiates a network
request and then returns. The completion handler is called later, when
the data is has arrived. Then you have to reload the table view with
tableView.reloadData()
after populating the data source array with the new data.
Second, in
for var i = 0; i < count; i++
{
self.elements.setObject(..., forKey: ...)
self.elements.setObject(..., forKey: ...)
// ...
self.posts.addObject(self.elements)
}
you are modifying the same dictionary again and again. NSMutableArray
and NSMutableDictionary are reference types, therefore you end up
with an array containing count pointers to the same dictionary
(containing the data from the last loop execution).
Instead of a having a single dictionary as property
var elements = NSMutableDictionary()
this should be a local variable in your loop:
for var i = 0; i < count; i++
{
var elements = NSMutableDictionary()
self.elements.setObject(..., forKey: ...)
self.elements.setObject(..., forKey: ...)
// ...
self.posts.addObject(elements)
}
You might also consider to use Swift arrays and dictionaries instead.
These are value types which makes this kind of error much less
likely.

Resources