Dynamic Cast Faild - AnyObject to NSArray - iphone

I am trying to read textFields values which is coming trough alert controller but I am getting run time error on casting with Anyobject to NSArray
alertUserStatus.addAction(UIAlertAction(title: "Sign Up", style: UIAlertActionStyle.Default, handler: {
alertAction in
//let textField:NSArray=alertUserStatus.textFields as AnyObject! as NSArray
let textField=alertUserStatus.textFields! as AnyObject! as NSArray
let username:String=textField.objectAtIndex(0) as String
let password:String=textField.objectAtIndex(1) as String
var signUpUser=PFUser()
signUpUser.username=username
signUpUser.password=password
signUpUser.signUpInBackgroundWithBlock{
(success:Bool!,error:NSError!)->Void in
if success == true {
println("sign up successfully")
}
else{
println("fail to sign up")
}
}
}))

It always helps to look at the type you are getting from a function before trying to massage it into another type. In this case alertUserStatus.textFields returns [AnyObject]? – that is, an optional array of AnyObject. So there’s really no need to convert it to an AnyObject and then to an NSArray just so you can get the first two entries from it. Instead you want to unwrap it and convert it to an array of String.
It’s also best to avoid using !, even if you’re confident the optional can’t possibly be nil, because one of these days there’ll be an edge case that you miss that doesn’t hold and your code will crash with an inexplicable force-unwrap error message.
// nb you can skip the handler: argument name with trailing-closure syntax
alertUserStatus.addAction(UIAlertAction(title: "Sign Up", style: UIAlertActionStyle.Default) {
alertAction in
if let textFields = alertUserStatus.textFields as? [String] where textFields.count > 1 {
let username = textFields[0]
let password = textFields[1]
// rest of your code...
}
else {
// assert with intelligible message or log error or similar
}
})

Related

How to check for quotations in swift 4?

I am trying to check for string if it contains a single quotation or a double quotation
I tried using the following code but when I test and enter in the text field a single or double quotation , it passes as if there is no quotation.
var usernameSQL = username.text?.contains("\'") as! Bool || username.text?.contains("\"") as! Bool
guard !usernameSQL else {
let alertVC = UIAlertController(title: "Invlalid symbols", message: "(') and (\") symbols are not allowed", preferredStyle: .alert)
alertVC.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.show(alertVC, sender: nil)
return
}
print("passed")
What I get is "passed" being printed instead of getting alert "Invalid symbols".
Thanks in advance.
I think you are overcomplicating things.
You can easily achieve the same result with this small refactor.
if let textValue = username.text {
if textValue.contains("\'") || textValue.contains("\"") {
print("Show Alert")
} else {
print("Passed")
}
} else {
print("username.text is nil do something or just ignore")
}
Hope it helps.
A smarter solution is to use CharacterSet and rangeOfCharacter(from:. The benefit is the optional doesn't need to be unwrapped.
If there are no quotes the expression returns nil and passes the guard.
let quoteSet = CharacterSet(charactersIn: "\'\"")
guard username.text?.rangeOfCharacter(from: quoteSet) == nil else { ...
However I would take Larme's advice and create the character set with the allowed characters.

Warnings from String interpolation

I've encountered a problem I can't solve myself. I have tried the Internet without any luck.
I'm still pretty new to Swift and coding, and right now following a guide helping me create an app.
Unfortunately, as I can understand, the app was written for Swift 3, and is giving me some issues since I'm using Swift 4.
I have to lines that gives me this warning:
String interpolation produces a debug description for an optional value; did you mean to make this explicit?
Use 'String(describing:)' to silence this warning Fix
Provide a default value to avoid this warning Fix
However, when I click one of Xcode's solutions I get another problem.
If I use the first fix, the app crashes and I get the following message:
Thread 1: Fatal error: Unexpected Segue Identifier;
If I use the second fix I have to assign a default value. And I don't know what this should be.
The whole passage of code is as follows.
It's the line starting with guard let selectedMealCell and the last one after default: that is causing the issues.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
switch(segue.identifier ?? "") {
case "AddItem":
os_log("Adding a new meal.", log: OSLog.default, type: .debug)
case "ShowDetail":
guard let mealDetailViewController = segue.destination as? MealViewController else {
fatalError("Unexpected destination: \(segue.destination)")
}
guard let selectedMealCell = sender as? MealTableViewCell else {
fatalError("Unexpected sender: \(sender)")
}
guard let indexPath = tableView.indexPath(for: selectedMealCell) else {
fatalError("The selected cell is not being displayed by the table")
}
let selectedMeal = meals[indexPath.row]
mealDetailViewController.meal = selectedMeal
default:
fatalError("Unexpected Segue Identifier; \(segue.identifier)")
}
}
So, the first suggested fix worked for you. It quieted the compile time warning, although admittedly String(describing:) is a weak solution.
In both cases, you need to unwrap the optional value. For the first case you should use:
guard let selectedMealCell = sender as? MealTableViewCell else {
if let sender = sender {
fatalError("Unexpected sender: \(sender))")
} else {
fatalError("sender is nil")
}
}
and in the second case:
fatalError("Unexpected Segue Identifier; \(segue.identifier ?? "")")
Then you got a runtime error:
"Unexpected Segue Identifier;"
That is telling you that your switch didn't match the first 2 cases and it ran the default case. The crash is caused because your code is explicitly calling fatalError. Your segue.identifier is apparently an empty string.
So your problem is actually in your Storyboard. You need to assign identifiers to your segues. Click on the segue arrows between your view controllers, and assign identifiers "AddItem" and "ShowDetail" to the proper segues. The segue identifier is assigned in the Attributes Inspector on the right in Xcode.
If you are prepared to write an small extension to Optional, it can make the business of inserting the value of an optional variable less painful, and avoid having to write optionalVar ?? "" repeatedly:
Given:
extension Optional: CustomStringConvertible {
public var description: String {
switch self {
case .some(let wrappedValue):
return "\(wrappedValue)"
default:
return "<nil>"
}
}
}
Then you can write:
var optionalWithValue: String? = "Maybe"
var optionalWithoutValue: String?
print("optionalWithValue is \(optionalWithValue.description)")
print("optionalWithoutValue is \(optionalWithoutValue.description)")
which gives:
optionalWithValue is Maybe
optionalWithoutValue is <nil>
You can also write print("value is \(anOptionalVariable)") -- the .description is redundant since print() uses CustomStringConvertible.description anyway -- but although it works you still get the annoying compiler warning.
You can use the following to automatically produce "nil" (or any other String) for nil values and for non-nil values use the description provided by CustomStringConvertible
extension String.StringInterpolation {
mutating func appendInterpolation<T: CustomStringConvertible>(_ value: T?) {
appendInterpolation(value ?? "nil" as CustomStringConvertible)
}
}
For your own types you have to conform to CustomStringConvertible for this to work:
class MyClass: CustomStringConvertible {
var description: String {
return "Whatever you want to print when you use MyClass in a string"
}
}
With this set up, you can simply use your optionals the same way as any other type, without any compiler warnings.
var myClass: MyClass?
myClass = MyClass()
print("myClass is \(myClass)")

Weak self in closures and consequences example

I have done abit of research on stackoverflow and apple's documentation about ARC and Weak/Unowned self (Shall we always use [unowned self] inside closure in Swift). I get the basic idea about strong reference cycle and how it is not good as they cause memory leaks. However, I am trying to get my head around when to use Weak/Unowned self in closures. Rather then going into the "theory", I think it would really really help if someone could kindly explain them in terms of the bottom three cases that I have. My questions is
Is it OK to put weak self in all of them (I think for case two there is no need because I saw somewhere that UIView is not associated with self?. However, what if I put weak self there, is there anything that can cause me headache?
Say if the answer is No, you cant put weak self everywhere in all three cases, what would happen if I do (example response would be much appreciated...For example, the program will crash when this VC ....
This is how I am planning to use weakSelf
Outside the closure, I put weak var weakSelf = self
Then replace all self in closure with weakSelf?
Is that OK to do?
Case 1:
FIRAuth.auth()?.signInWithCredential(credential, completion: { (user: FIRUser?, error: NSError?) in
self.activityIndicatorEnd()
self.performSegueWithIdentifier(SEGUE_DISCOVER_VC, sender: self)
})
Case 2:
UIView.addKeyframeWithRelativeStartTime(0.0, relativeDuration: 0.1, animations: {
self.messageLbl.alpha = 0.5
})
Case 3:
//checkUserLoggedIn sends a request to firebase and waits for a response to see if the user is still authorised
checkUserLoggedIn { (success) in
if success == false {
// We should go back to login VC automatically
} else {
self.discoverTableView.delegate = self
self.discoverTableView.dataSource = self
// Create dropdown menu
let menuView = BTNavigationDropdownMenu(navigationController: self.navigationController, title: self.dropDownItems.first!, items: self.dropDownItems)
menuView.didSelectItemAtIndexHandler = {[weak self] (indexPath: Int) -> () in
if indexPath == 0 {
self?.mode = .Closest
self?.sortByDistance()
} else if indexPath == 1 {
self?.mode = .Popular
self?.sortByPopularity()
} else if indexPath == 2 {
self?.mode = .MyPosts
self?.loadMyPosts()
} else {
print("Shouldnt get here saoihasiof")
}
}
// Xib
let nib = UINib(nibName: "TableSectionHeader", bundle: nil)
self.xibRef = nib.instantiateWithOwner(self, options: nil)[0] as? TableSectionHeader
self.discoverTableView.registerNib(nib, forHeaderFooterViewReuseIdentifier: "TableSectionHeader")
// Set location Manager data
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
// Check location service status
if self.locationAuthStatus == CLAuthorizationStatus.AuthorizedWhenInUse {
// Already authorised
self.displayMessage.hidden = false
} else if self.locationAuthStatus == CLAuthorizationStatus.NotDetermined {
// Have not asked for location service before
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("LocationVC") as! LocationVC
vc.locationVCDelegate = self
self.presentViewController(vc, animated: true, completion: nil)
} else {
let alertController = UIAlertController(title: "Enable Location", message: "location is required to load nearby posts", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
let settingsAction = UIAlertAction(title: "Settings", style: .Default, handler: { (action: UIAlertAction) in
let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString)
if let url = settingsUrl {
UIApplication.sharedApplication().openURL(url)
}
})
alertController.addAction(settingsAction)
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true, completion: nil)
self.displayMessage.hidden = false
self.displayMessage.text = "Could not determine your location to find nearby posts. Please enable location Service from settings"
}
// Styling
self.refreshBtn.tintColor = COLOR_NAVIGATION_BUTTONS
self.discoverTableView.backgroundColor = COLOR_DISCOVERVC_TABLEVIEW_BACKGROUND
// Allow navigation bar to hide when scrolling down
self.hidingNavBarManager = HidingNavigationBarManager(viewController: self, scrollView: self.discoverTableView)
// Allow location to start updating as soon as we have permission
self.locationManager.startUpdatingLocation()
}
}
--Update--
Most of my code looks like case 3 where everything is wrapped inside a closure that either check if there is internet connectivity before any of the action is taken place. So I might have weak self everywhere??
--Update 2--
Case 4:
// The haveInternetConnectivity function checks to see if we can reach google within 20 seconds and return true if we can
haveInternetConnectivity { (success) in
if success == false {
self.dismissViewControllerAnimated()
} else {
self.label.text = "You are logged in"
self.performSegueWithIdentifier("GoToNextVC")
}
}
Question about case 4.
Am I correct to say that even though this closure does not have weak/unowned self, it will never create strong reference (and memory leak) because even if the VC is dismissed before the completion block is executed, Xcode will try to run the code inside the completion block when we have confirmed internet status and will just do nothing (No crash) because self doesn't exist anymore. And once the code reached the last line inside the closure, the strong reference to self would be destroyed hence deallocate the VC?
So putting [weak Self] in that case would just mean that xcode would ignore those lines (as oppose to try and run it and nothing happens) which would mean a better practice but no issues on my hand either way
The question should not be "can I use weak reference," but rather "should I use weak reference." You use weak references to avoid strong reference cycles or to keep a closure from hanging on to something after it may have been disposed. But don't just add weak references because you can.
In case 1, you probably do want to use [weak self]. Why? Because if the view controller was dismissed while the authorization was underway, do you really want to keep a reference to a view controller that was dismissed? Probably not in this case.
In case 2, you theoretically could use [weak self] in the animation block, but why would you? There's no reason to. The weak reference is something you do with completion handlers and/or closure variables, but for an animation block it offers no utility, so I wouldn't do it there. To use weak here suggests a misunderstanding of the memory semantics involved.
In case 3, you have two separate issues.
In the didSelectItemAtIndexHandler, that probably should use [unowned self] because the object's own closure is referring to itself.
It may be a moot issue, as I don't see you actually using that BTNavigationDropdownMenu (perhaps that initializer is adding itself to the navigation controller, but that's not a well designed initializer if so, IMHO).
But as a general concept, when an object has a handler closure that can only be called when the object is still around, but shouldn't, itself, cause the object to be retained, you'd use [unowned self].
In the broader checkUserLoggedIn closure, the question is whether that's a completion handler. If so, you probably should use [weak self], because this could be initiated and be running by the time self is dismissed, and you don't want to have checkUserLoggedIn keep a reference to a view controller that was dismissed. But you wouldn't want to use [unowned self] because that would leave you with dangling pointers if it had been released by the time the closure runs.
As an aside, you contemplate:
weak var weakSelf = self
That is a bit unswifty. You would use the [weak self] pattern at the start of the checkUserLoggedIn closure. If you have an example where you're tempted to use weak var weakSelf = ..., you should edit your question, including an example of where you want to use that pattern. But this is not one of those cases.

Conversion to Swift 2 NSIndexPath error

Problem: Upon converting to Swift 2 I get the following error: "Value of optional type [NSIndexPath]? not unwrapped, did you mean to use "!" or "?"". The issue is that if I use '?', it gives an error saying I should use '!', and if I use '!' it gives an error saying I should use '?'. Thus it creates this nasty little bug loop that seems to be unfixable.
Code:
override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
if identifier == Constants.SegueIdentifier {
if let selectedRowIndex = collectionView?.indexPathsForSelectedItems().last as? NSIndexPath {
if let cell = collectionView?.cellForItemAtIndexPath(selectedRowIndex) {
//We check if the selected Card is the one in the middle to open the chat. If it's not, we scroll to the side card selected.
if cell.frame.size.height > cell.bounds.size.height {
return true
} else {
collectionView?.scrollToItemAtIndexPath(selectedRowIndex, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
return false
}
}
}
}
return true
}
I haven't been able to come up with any work arounds since it seems like I need to somehow unwrap it. Has anyone seen this problem?
indexPathsForSelectedItems() returns [NSIndexPath]? (optional), you have to add another question mark for optional chaining and remove as? NSIndexPath as the compiler knows the unwrapped type.
if let selectedRowIndex = collectionView?.indexPathsForSelectedItems()?.last {
Solution:
if let selectedRowIndex = collectionView!.indexPathsForSelectedItems()!.last! as? NSIndexPath
My only concern is type safety but it works in my project as it currently is.

App crashing because of no values in the uiTextfield

When I enter a value in the UiTextField and press add button the app works perfectly fine,but when I don't enter a value in the UiTextField, then press the add button the whole app crashes. can someone please help me and show me the light.Thank you in advance and good wishes for you and your family.
This is my code
import UIKit
class ViewController: UIViewController,UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.textFild.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet var textFild: UITextField!
#IBAction func ButtonAwesome(sender: AnyObject) {
let value:Double = Double(textFild.text!)!
let sum:Double = value - 1;
let alert = UIAlertController(title: "km", message: "\(sum)", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Exit", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert,animated: true, completion: nil)
}
func textField(textField: UITextField,shouldChangeCharactersInRange range: NSRange,replacementString stringer: String) -> Bool
{
if (textField.text == "" && (stringer == ".")) {
return false;
}
let countdots = textField.text!.componentsSeparatedByString(".").count - 1
if countdots > 0 && stringer == "."
{
return false
}
return true
}
}
A more robust solution would be to use the nil coalescing operator to assert that the initialization of value never fails.
#IBAction func ButtonAwesome(sender: AnyObject) {
let value = Double(textFild.text ?? "0") ?? 0
let sum:Double = value - 1;
let alert = UIAlertController(title: "km", message: "\(sum)", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Exit", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert,animated: true, completion: nil)
}
This is valid for any of the following "unexpected" values of textField.text: nil, "" (empty) or some character that cannot be used to initialize a Double type variable (e.g. "a"). For all these cases, value is give a value of 0.
As an example, consider the following comparisons between a safe and an un-safe way of solving your initial runtime exception.
We first look at the dangers of forcibly unwrapping optionals - not a safe solution. What if textField.text contains nil or an non-numerical character, e.g. "a"? Result:
var myTextFieldText : String? = nil
/* Example 1: nil value causes runtime error */
if myTextFieldText != "" { // this is 'true' -> enter if closure
let value:Double = Double(myTextFieldText!)!
/* this will fail at run time since we are forcibly (un-safely)
unwrapping an optional containing 'nil' */
}
/* Example 2: non-numeric string character causes runtime error */
myTextFieldText = "a"
if myTextFieldText != "" { // this is 'true' -> enter if closure
let value:Double = Double(myTextFieldText!)!
/* this will fail at run time since we cannot initalize a
Double with a string value "a", hence 'Double(myTextFieldText!)'
returns nil, but since we've appended '!', we, like above,
forcibly tries to unwrap this optional of value nil */
}
You should, generally, always use conditional unwrapping of optionals, to avoid encountering a nil value when forcibly unwrapping optionals, the latter leading to a runtime error.
A more robust version of the example above, making use of the nil coalescing operator:
myTextFieldText = nil
let valueA = Double(myTextFieldText ?? "0") ?? 0 // 0
/* OK, Double(myTextFieldText ?? "0") = Double("0") = 0 */
myTextFieldText = "a"
let valueB = Double(myTextFieldText ?? "0") ?? 0 // 0
/* OK, Double(myTextFieldText ?? "0") = Double("a") = nil (fails)
=> use rhs of outer '??' operator: nil ?? 0 = 0 */
For an alternative method, where you extend UITextField to cover your string to numerical type conversion needs, see Leos neat answer in the following thread:
Swift - Converting String to Int
The thread also contains some other valuable insights w.r.t. reading text as numerical values from UITextField instances.
Finally, when dealing with String to Double value conversion, it might be appropriate to use a fixed precision w.r.t. number of decimal digits in your resulting Double value. For a thorough example of how to do this using NSNumberFormatter and extensions, see:
How to input currency format on a text field (from right to left) using Swift?
Please replace your buttonAwesome method with the following code where you check if textfield is not empty, this will work if textfield has a value:
#IBAction func ButtonAwesome(sender: AnyObject) {
if textFild.text != "" {
let value:Double = Double(textFild.text!)!
let sum:Double = value - 1;
let alert = UIAlertController(title: "km", message: "\(sum)", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Exit", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert,animated: true, completion: nil)
}
}
Since textfield.text is an optional value and as in your case textfield can or cannot have text. So you should test for optional as give below.
if let textIsAvailable = textfield.text
{
print("Text \(textIsAvailable)")
}
Note : The reason for your crash is you are trying to access value which actually doesn't have any value.