How to avoid this Force Cast - swift

I think that a force cast I have in my app is causing it to crash, (userDefaults.value(forKey: "timeDiffSecondsDefault") as! Int?)...but I really don't know how to avoid it. Any guidance is greatly appreciated!
func getProductionTime(store: Bool = false) {
let userDefaults = UserDefaults.standard
let productionTimeFormatter = DateFormatter()
productionTimeFormatter.timeZone = TimeZone(abbreviation: defaultTimeZone)
productionTimeFormatter.dateFormat = defaultTimeFormat
if let defaultTimeDiffSeconds: Int = userDefaults.value(forKey: "timeDiffSecondsDefault") as! Int? {
timeDiffSeconds = defaultTimeDiffSeconds
}
let productionTime = Calendar.current.date(byAdding: .second, value: timeDiffSeconds, to: Date())!
if store {
storeDateComponents(nowProdTime: productionTime)
}
productionTimeString = productionTimeFormatter.string(from: productionTime)
liveCounterButton.setTitle(productionTimeString, for: .normal)
}

Use the dedicated API which returns a non-optional
timeDiffSeconds = userDefaults.integer(forKey: "timeDiffSecondsDefault")
If a default value != 0 is required register it.
Note: Never use value(forKey with UserDefaults unless you really need KVC

When the key is absent, you are trying to force-cast an empty Any? to Int?, and thus, the if condition is not executed:
if let defaultTimeDiffSeconds: Int = userDefaults.value(forKey: "timeDiffSecondsDefault") as! Int? {
timeDiffSeconds = defaultTimeDiffSeconds
}
And if timeDiffSeconds was not initialized elsewhere, it will cause the crash when you try to use it.
The appropriate way would be conditional casting with as?:
if let defaultTimeDiffSeconds = userDefaults.object(forKey: "timeDiffSecondsDefault") as? Int { ... }
object(forKey:) was kindly suggested by Mr Leonardo.
Using userDefaults.integer(forKey: "timeDiffSecondsDefault") might be confusing when using timeDiffSeconds later, since integer(forKey:) would return 0 if the key is absent in user defaults, and returns an integer even if the value is a string or a boolean.

Related

How to safely force unwrap time in dictionary

I have an issue. I have a dictionary type [String: Any]
my code that works is
dict["start"] = "\(start.hour!):\(start.minute!)"
if let end = end {
dict["end"] = "\(end.hour!):\(end.minute!)"
}
But as I use swiftlint it throws me an error for force unwrapping. Value must be saved so if let is not good here :)
that is mostly a semantic issue, but you could do something like this:
if let startHour = start.hour,
let startMinute = start.minute {
dict["start"] = "\(startHour):\(startMinute)"
if let end = end,
let endHour = end.hour,
let endMinute = end.minute {
dict["end"] = "\(endHour):\(endMinute)"
}
}
...or something similar – as there are various ways in Swift to safely unwrap an optional.
You can try the following demo code. I hope it will help you.
import Foundation
let calendar = Calendar.current
let dateComponents = DateComponents(hour: 12, minute: 20, second: 55)
func getHoursMinutesFrom(time: DateComponents) -> (hour: Int,minute: Int) {
switch (time.hour, time.minute) {
case let (.some(hour), .some(minutes)):
return (hour,minutes)
default:
return (0,0)
}
}
print(getHoursMinutesFrom(time: dateComponents))

Why is this constant from a Dictionary not unwrapped?

I'm using some base swift functionality, the fact that you can unwrapped Optional values with a if. I am working on swift 4 and Xcode 10.3
let xyz: String? = nil
if let unwrappedValue = xyz {
print("This is unwrapped")
} else {
print("This is not unwrapped") // Program goes there
}
I have made a dictionary with an Optional value as an element, and when I try to unwrap it, it doesn't unwrap the variable, but keeps the Optional value.
var referenceDatesIdentifiers: [String: Date?] =
["ReferenceDateIdentifier": nil]
if let referenceDate = referenceDatesIdentifiers["ReferenceDateIdentifier"] {
referenceDateLabel.text = otherDateFormater.string(from: referenceDate!) // ReferenceDate is Optional and not unwrapped
}
Why isn't referenceDate unwrapped ?
I tried to export the elem of the Dictionary in a constant, but same problem, it's not unwrapped
let refDateOpt = referenceDatesIdentifiers["ReferenceDateIdentifier"]
if let referenceDate = refDateOpt {
referenceDateLabel.text = otherDateFormater.string(from: referenceDate!) // Reference date is also Optional and not unwrapped
}
What is happening ?
You should required again optionally unwrap date with type casting will fix your issue. Update code as follow:
if let referenceDate = referenceDatesIdentifiers["ReferenceDateIdentifier"] as? Date {
print(referenceDate)
}
The problem here is that it doesn't makes any sense to create a dictionary with a nil value. So instead of checking if the key exists before adding a value, I just add the value and check the value with a if let
In the end, this was the answer
var referenceDatesIdentifiers: [String : Date] = [:]
if let referenceDate = referenceDatesIdentifiers["ReferenceDateIdentifier"] {
referenceDateLabel.text = otherDateFormater.string(from: referenceDate)
}
You could use below way. It will work perfectly.Update code as follow:
var referenceDatesIdentifiers: [String: Date?] =
["ReferenceDateIdentifier": nil]
if let refDateOpt = referenceDatesIdentifiers["ReferenceDateIdentifier"] as? Date {
print(refDateOpt)
}
or
if let refDateOpt = referenceDatesIdentifiers["ReferenceDateIdentifier"]{
if let referenceDate = refDateOpt {
print(referenceDate)
}
}

Init has been renamed to init(describing) error in Swift 3

This code works fine in Swift 2:
guard let userData = responseData["UserProfile"] as? [String : AnyObject] else { return }
var userProfileFieldsDict = [String: String]()
if let profileUsername = userData["Username"] as? NSString {
userProfileFieldsDict["username"] = String(profileUsername)
}
if let profileReputationpoints = userData["ReputationPoints"] as? NSNumber {
userProfileFieldsDict["reputation"] = String(profileReputationpoints)
}
But, in Swift 3 it throws an error on userProfileFieldsDict["reputation"] saying
init has been renamed to init(describing:)
My question is why does it trigger on that line and not on the userProfileFieldsDict["username"] assignment line, and how to go about fixing it? I'm assuming it's because I'm casting a NSNumber to a String, but I can't really understand why that matters.
NSNumber is a very generic class. It can be anything from a bool to a long to even a char. So the compiler is really not sure of the exact data type hence it's not able to call the right String constructor.
Instead use the String(describing: ) constructor as shown below
userProfileFieldsDict["reputation"] = String(describing: profileReputationpoints)
Here's more info about it.
You need to drop your use of Objective-C types. This was always a bad habit, and now the chickens have come home to roost. Don't cast to NSString and NSNumber. Cast to String and to the actual numeric type. Example:
if let profileUsername = userData["Username"] as? String {
userProfileFieldsDict["username"] = profileUsername
}
if let profileReputationpoints = userData["ReputationPoints"] as? Int { // or whatever
userProfileFieldsDict["reputation"] = String(profileReputationpoints)
}

Comparing non optional value leads to error

I am on point where I gotta compare non optional value with nil. But I can't do it because Xcode says:
Comparing non-optional value of type 'Int' to nil always returns false
So I created Struct and then made variable: var products: [Product] = []
How I am able to compare it with nil?:
if products[indexPath.row].snusPortions == nil
{
cell.snusPortionsAmountLabel.text = "N/A"
}else
{
cell.snusPortionsAmountLabel.text = String(products[indexPath.row].snusPortions)
}
I've assigned values to them like this:
let ref = FIRDatabase.database().reference().child("Snuses").queryOrdered(byChild: "Brand").queryEqual(toValue: brandName)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists(){
let enumerator = snapshot.children
while let thisProduct = enumerator.nextObject() as? FIRDataSnapshot
{
print(thisProduct.value) // So I may see what the data is like and know how to extract it
// Chances are you'd have to create a dictionary
let thisProductDict = thisProduct.value as! [String:AnyObject]
let productName = thisProductDict["Products"] as! String
let snusPortions = thisProductDict["PortionsCan"] as? Int
let productObject = Product(snusProductTitle: productName, snusNicotine: snusNicotine, snusPortions: snusPortions!, snusFlavor: snusFlavor, snusWeight: snusWeight!, snusShippingWeight: snusShippingWeight, snusProductImageURL: productURL)
self.products.append(productObject)
print(self.products)
}
self.tableView.reloadData()
}
})
This is Product struct:
struct Product {
var snusProductTitle: String
init()
{
snusProductTitle = ""
}
init(snusProductTitle: String){
self.snusProductTitle = snusProductTitle
}
}
While testing it says snusPortions is nil but I said to make it "N/A" if it is nil, why?
It sounds like you are confusing yourself between the local variable snusPortions and the Product property snusPortions.
In your Product definition, the property snusPortions is an Int. It can never be nil. Hence, in this code:
if products[indexPath.row].snusPortions == nil
... this Product's snusPortions will never be nil, and we will never set the text to "N/A".
Now let's look at your other code:
let snusPortions = thisProductDict["PortionsCan"] as? Int
This is a completely different snusPortions. It can be nil, namely, if thisProductDict lacks a "PortionsCan" key or if its value is not castable to Int.

EXC Bad Instruction

In was wondering why I keep getting this error message, EXC Bad Instruction could someone help me out and tell me why.
Here is the code.
func updateStocks() {
let stockManager:StockManagerSingleton = StockManagerSingleton.sharedInstance
stockManager.updateListOfSymbols(stocks)
//Repeat this method after 15 secs. (For simplicity of the tutorial we are not cancelling it never)
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(15 * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(),
{
self.updateStocks()
}
)
}
//4
func stocksUpdated(notification: NSNotification) {
let values = (notification.userInfo as! Dictionary<String,NSArray>)
let stocksReceived:NSArray = values[kNotificationStocksUpdated]!
stocks.removeAll(keepCapacity: false)
for quote in stocksReceived {
let quoteDict:NSDictionary = quote as! NSDictionary
let changeInPercentString = quoteDict["ChangeInPercent"] as! String
let changeInPercentStringClean: NSString = (changeInPercentString as NSString).substringToIndex((changeInPercentString as NSString).length-1)
stocks.append(quoteDict["symbol"] as! String,changeInPercentStringClean.doubleValue)
}
tableView.reloadData()
NSLog("Symbols Values updated :)")
}
}
The line with the error in it is,
let changeInPercentString = quoteDict["ChangeInPercent"] as! String
The error states that Swift attempted to unwrap a nil value, as you stated on this line
let changeInPercentString = quoteDict["ChangeInPercent"] as! String
Swift attempts to force setting the value of quoteDict["ChangeInPercent"] to a String, because you use as!, instead, you should use as?, which will set the value to nil if the value cannot be found
let changeInPercentString = quoteDict["ChangeInPercent"] as? String
You could set this to a default value by using the ?? operator. For example, if you wanted the default value to be 0.0%, you could use
let changeInPercentString = (quoteDict["ChangeInPercent"] as? String) ?? "0.0%"
The inherent problem is most likely either that quoteDict["ChangeInPercent"] does not exist, or quoteDict["ChangeInPercent"] is not a String - it may be an NSString or simply a Double value.
If you find out that it is supposed to be an NSString, for example, you will need to change how you cast the value
let changeInPercentString: NSString = (quoteDict["ChangeInPercent"] as? NSString) ?? "0.0%"