Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have been playing around with Swift as I'm new to iOS Development and really enjoying getting to know the language. Recently I have started to look at installing third party plugins etc and I found one called SwiftyJson (https://github.com/SwiftyJSON/SwiftyJSON) I have managed to get it to work using this script:
request(.GET, url, parameters: parameters)
.responseJSON { (req, res, json, error) in
if(error != nil) {
NSLog("Error: \(error)")
println(req)
println(res)
}
else {
NSLog("Success: \(url)")
var json = JSON(json!)
println(json)
}
}
I would like to know two things: 1) Is it possible to get the list of results from the JSON provided into a UITableView like so:
Item 1
----------------------------
Item 2
----------------------------
Etc.. and 2) How would I do that?
Edit: Json
Sorry about image - I couldn't copy it because of a stupid chrome extension I have:
Yes it is possible, though you aren't really using the important features of SwiftyJSON in this code sample. The most straightforward way would be to create the tableview the same as you would for anything, likely using an array as your datasource. Once you've received your JSON you still need to parse it and create the objects for your array. Perhaps if you included a snippet of the JSON, it would be easier to help.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a JSON data in my local file, I want to replicate same in my form.
JSON data tell us info like, which field is required, which is dropdown, placeholder and more info.
Almost I achieved my goal, but only the thing is keyboard taking double taps to show when I am switching the text fields.
I was stuck on this issue. Can anyone help me?
I cant explain the issue with the small code, that's why adding complete source code, so please excuse.
here I am adding my source code link,
https://drive.google.com/file/d/12vhrz6CgDSuma6ViYOsGkCIb9SE6fSbR/view
Try using :
func updateModel(text: String, indexPath: IndexPath) {
var item = self.viewModel.get(at: indexPath)
item.value = text
item.indexPath = indexPath
self.viewModel.formModel[indexPath.section].items![indexPath.row] = item
self.tableForm?.reloadRows(at: [indexPath], with: .automatic)
}
Also, avoid retain cycle using weak self :
self.viewModel.fetchFormData(fileName: "fields") { [weak self] (data, error) in
if (error?.isEmpty)! {
self?.tableForm?.reloadData()
}
}
Otherwise ViewController & ViewModelForm will be kept in memory forever
In updateModel function you need to replace
self.tableView.reloadData()
with
self.tableForm?.reloadRows(at: [indexPath], with: .automatic)
So the basic rule says if you only wish to reload a single row in table view use reloadRows instead of reloading the complete table view.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I would like to open Google Maps app on iOS using the url scheme for showing directions with multiple stops.
The web url for testing is:
https://google.com/maps/dir//49.54643774,22.28223445/49.54679476,22.28170513/49.54726735,22.28154318/49.54760869,22.28156607/49.54820312,22.2815506/49.54856556,22.28146425/49.54907329,22.28133231/49.54989807,22.28207924/49.55017454,22.2824851/49.55064392,22.28306989/49.5508548,22.28325003/49.55143275,22.28381447/49.55169439,22.28410868/49.5520271,22.28443534
I tried many configurations of the app scheme using comgooglemaps:// without success.
You can read the Google Maps documentation for opening the app.
To simplify it, this is the format you need to follow:
comgooglemaps://?saddr=Google,+1600+Amphitheatre+Parkway,+Mountain+View,+CA+94043&daddr=Google+Inc,+345+Spear+Street,+San+Francisco,+CA¢er=37.422185,-122.083898&zoom=10
in swift you would do something like this, take note of the callback option, you can choose to not have it if you don't want to return to your app:
let testURL = URL(string: "comgooglemaps-x-callback://")!
if UIApplication.shared.canOpenURL(testURL) {
let directionsRequest = "comgooglemaps-x-callback://" +
"?daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York" +
"&x-success=sourceapp://?resume=true&x-source=AirApp"
let directionsURL = URL(string: directionsRequest)!
UIApplication.shared.openURL(directionsURL)
} else {
NSLog("Can't use comgooglemaps-x-callback:// on this device.")
}
Edit: To use coordinates, use it this way:
comgooglemaps://?saddr=52.3668563,4.8890813&daddr=52.357516,4.902319&zoom=10
Edit 2: For for more points on the map append coordinate using +to:Latitude,Longtitude to the daddr parameter
comgooglemaps://?saddr=52.3668563,4.8890813&daddr=52.357516,4.902319+to:52.357786,4.891913&zoom=10
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
in my iOS app, i have a simple if statement like this:
if (responseString! != "No Data") {
print("Data available")
}
if I print the responseString, this is the result:
No Data
But the print "Data available" will shown as well.
why?
There might be whitespaces or newlines in your responseString, try this:
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)?.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have a project that polls a URL that returns a JSON format values. Any recommendations of whats the best way to parse the result for my iOS app?
First of all there is no such thing as best way. If there was a best way, you will probably heard it or find it among top google hits.
You can make do-it-yourself with NSJSONSerialization. This what Apple provides, and it is merely the fastest and the hardest to use. It's not even that 'hard', it just get's complicated when JSON has manny sub-levels.
What I can recommend you is SwiftyJSON. It had minor(barely noticable in most apps) overhead but it's much more easier to use in Swift. A great example is found on raywenderlich site.
You can also simply parse
var data = NSData(contentsOfURL: NSURL(string: "http://api.androidhive.info/contacts/")!)
var parcedData : NSMutableDictionary = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments, error: nil) as! NSMutableDictionary
print(parceData)
There are many other ways to do it.
You can use Alamofire with SwiftyJSON
Snippet With Alamofire and SwiftyJSON
Alamofire.request(.GET, url, parameters: parameters)
.responseJSON { (req, res, json, error) in
if(error != nil) {
NSLog("Error: \(error)")
println(req)
println(res)
}
else {
NSLog("Success: \(url)")
var json = JSON(json!)
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I'm trying to use the JSONKIt found here https://github.com/johnezang/JSONKit to parse through a JSON Feed and put into objective-c objects. I'm new at iOS and don't really know where to start. Are there any good tutorials for using this library?
After googling, I didn't find any tutorials but using JSONKit should be self explanatory.
After downloading your JSON feed using NSURLConnection or ASIHTTPRequest simply create a dictionary of all the objects in the JSON feed like so:
//jsonString is your downloaded string JSON Feed
NSDictionary *deserializedData = [jsonString objectFromJSONString];
//Helpful snippet to log all the deserialized objects and their keys
NSLog(#"%#", [deserializedData description]);
After creating a dictionary you can simply do something like this:
NSString *string = [deserializedData objectForKey:#"someJSONKey"];
And that is the basics behind JSONKit.
JSONKit is much more powerful of course, you can find some of the other things you can do with it in JSONKit.h
I would becareful about making the assumption that objectFromJSONString is returns an NSDictionary, it can very well return an array, or nil, especially if the server returns some rarely used and thought of error.
A more appropriate action would be:
NSError *error;
id rawData = [jsonString objectFromJSONStringWithParseOptions:JKParseOptionNone error:&error];
if ( error != nil ) {
// evaluate the error and handle appropriately
}
if ( [rawData isKindOfClass:[NSDictionary class]] ) {
// process dictionary
}
else if ( [rawData isKindOfClass:[NSArray class]] ) {
// process array
}
else {
// someting else happened, 'rawData' is likely 'nil'
// handle appropriately
}
Without these checks, you could very well end up with a runtime error because the server returned someting unexpected.