swift if statement check string not correctly [closed] - ios

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())

Related

Firebase Fetching Data in Swift [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I can fetch data from firebase console. But how to get node from the database.
Please refer to below code :
let current_uid = FIRAuth.auth()?.currentUser?.uid
self.userRef?.queryOrdered(byChild: "Email").observe(FIRDataEventType.childAdded) { (snapshot : FIRDataSnapshot) in
let value = snapshot.value as? NSDictionary
let email = value?["Email"] as? String
let password = value?["Password"] as? String
print("User Email \(email!) and User Password \(password!)")
}
But i am bit confuse how to get 4dS6fdWy4of8VyDN8eouwqIk38j2 remember i have to fetch all the users from the database.
Any help will be appreciated.

What path should be used to write to iPhone's memory? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
i am trying to save a string to device's memory for a simple to do app. It works great on a simulator where i set the path for writing to memory to be - let path: String = directory + "App4.txt". But when running this on an actual iPhone it doesn't save to memory.
This is the function.
if let directory: String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
let path: String = directory + "App4.txt"
userInputString = userInput.joinWithSeparator("-")
// Writing.
do {
try userInputString.writeToFile(path, atomically: false, encoding: NSUTF8StringEncoding)
}
catch {
print("Error")// Error handling here.
}
}
Any help is very much appreciated!! :)
As #Martin-R is alluding to, you need to have a "/" in your path between directory and the file name.

Whats the best way to parse JSON format in iOS Swift? [closed]

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!)
}
}

Swift: Create table of results from SwiftyJson Results [closed]

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.

SWIFT: Why I can't get the current URL loaded in UIWebView? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I need to get the current URL loaded into the webview and this is how I'm trying to get that but it gives me this error: "Cannot convert the exporessions type 'ST7??' to type 'String'
and this is the code
var currentURL : NSString = webView.request?.URL.absoluteString!
What is wrong with this?
If you put parentheses around this, the error goes away:
let currentURL : NSString = (webView.request?.URL.absoluteString)!
Beware that yours might not be just a syntax problem.
If your webView is in a state where request == nil your app will crash at runtime.
I'd rather write something like:
if let currentURL = webView.request?.URL.absoluteString {
// do things ...
// Your currentURL will be automatically bridged to Swift's String type
} else {
// Just in case request is nil ...
}

Resources