Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
what is wrong with xcode? Get nil for key 9.0
#IBAction func sliderAction(_ sender: UISlider) {
speed = round(sender.value / step) * step
sender.value = speed
speedLabel.text = modeDict[sender.value]
print(sender.value)
print(speedLabel.text)
}
let modeDict : [Float : String] = [ 0.8 : "Pls delete game, you noob", 0.9 : "Pff, girl", 1.0 : "Optimal", 1.1 : "Very Hard", 1.2 : "Almost Unreal"]
let step : Float = 0.1
var speed : Float = 0.0
//
Print so, why 9.0 -> nil ?
0.8
Optional("Pls delete game, you noob")
0.9
nil
1.0
Optional("Optimal")
1.1
Optional("Very Hard")
1.2
Optional("Almost Unreal")
It's better to shift keys by 10
let modeDict : [Float : String] = [ 8 : "Pls delete game, you noob", 9 : "Pff, girl", 10 : "Optimal", 11 : "Very Hard", 12 : "Almost Unreal"]
and use
speedLabel.text = modeDict[sender.value*10]
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 2 years ago.
Improve this question
The following code allows to create a font with different weights.
func makeFont(weight: CGFloat, size: CGFloat) -> UIFont {
var attributesDict = [String: Any]()
attributesDict["Weight"] = weight
/* Rubik-Light - is a variable font */
let fontDescriptor = UIFontDescriptor(
fontAttributes: [
UIFontDescriptor.AttributeName.name : "Rubik-Light",
kCTFontVariationAttribute as UIFontDescriptor.AttributeName : attributesDict
]
)
return UIFont(descriptor: fontDescriptor, size: size)
}
It works fine on ios 13 and below, but doesn't work on iOS 14. Is there any solution?
Solved.
iOS 14 expects attribute ID instead of its name ("Weight").
So, attributeDict should look like this:
var attributesDict = [NSNumber: Any]()
attributesDict[NSNumber(value: 2003265652)] = weight
Attribute ID can be obtained as follows:
let variationAxes = (CTFontCopyVariationAxes(ctFont)! as Array)
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 2 years ago.
Improve this question
I need to check preconditions for proceeding a function in iOS/Swift.
Option 1:
guard let name = str["name"], let age = str["age"] else {
print("name/age missing")
return
}
Option 2:
guard let name = str["name"] else {
print("name missing")
return
}
guard let age = str["age"] else {
print("age missing")
return
}
Which option is recommended.
This is completely unrelated to Swift.
From a UI / UX perspective certainly the 2nd option since you can now point to the exact input field that is missing.
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 2 years ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
How can I check if the string is base64 encoded in swift?
Input = "tNC6umcfBS/gelbo2VJF3i4LAhUKMp4oDHWN5KyYUTWeJIQKKYx6oAcQnGncIrPJNC1tUYMKV4kJQj3q9voIOrxc1n7FmRFvDXeRgWGNcGYO66dH3VjoEgF0oxZOpfzwSZKSv3Jm7Q=="
let base64Regex = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$"
let predicate = NSPredicate(format: "SELF MATCHES %#", base64Regex)
let result = predicate.evaluate(with: "InputString")
You can also try to decode using the built-in Data:
let inputString = "..."
let isBase64Encoded = Data(base64Encoded: inputString) != nil
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 4 years ago.
Improve this question
I've already seen other questions asking about how many times the app has been opened. I want to send a local notification when the user uses the app for 31 consecutive days.
Would this be a NSUserDefaults discovery method or would I need to use an analytics API?
Use UserDefault. In appdelegate's didFinishLaunch method check for days count
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let kLastUsed = "LastUsedTime"
let kDaysCount = "DaysCount"
let currentDateTimeInterval = Int(Date().timeIntervalSinceReferenceDate)
var storedDaysCount:Int = UserDefaults.standard.integer(forKey: kDaysCount)
if storedDaysCount >= 31 {
//show pushNotifications
}
else {
let lastDateTimeInterval = UserDefaults.standard.integer(forKey: kLastUsed)
let diff = currentDateTimeInterval - lastDateTimeInterval
if diff > 86400 && diff < 172800 {
//next day. increase day count by one
storedDaysCount = storedDaysCount + 1
UserDefaults.standard.set(storedDaysCount, forKey: kDaysCount)
}
else if diff > 86400 {
//not next day. reset counter to 1
UserDefaults.standard.set(1, forKey: kDaysCount)
}
UserDefaults.standard.set(currentDateTimeInterval, forKey: kLastUsed)
}
return true
}
Just expanding on Hitesh's awesome answer to make it more suitable for realtime testing.
You cannot change the date in the simulator settings like you can on a real device. And if you change the date on your real device you might get some Apple server-Xcode syncing issues and Xcode will ask you to register your device in the Developer Portal again.
*Test on a real device using the current time because the UserDefaults needs the date and store from the real device.
To test for minutes or seconds just change all the Ints to Doubles and change the condition to something finer like if storedDaysCount >= 0.0000000015.
let kLastUsed = "LastUsedTime"
let kDaysCount = "DaysCount"
let currentDateTimeInterval = Double(Date().timeIntervalSinceReferenceDate)
var storedDaysCount:Double = UserDefaults.standard.double(forKey: kDaysCount)
if storedDaysCount >= 0.000000000015 {
print("storedDaysCount = \(storedDaysCount)")
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 7 years ago.
Improve this question
I have the following object:
(rates)
{
EUR: "1"
RON: 4.5
USD: 1.3
.
.
.
n: INT/STRING
}
Is there any function that does this?
if you care about the index, you can use a 'traditional' for loop - although #Eric points out this will soon be completely removed
for var i = 0; i < rates.count; i++
{
let rate = rates[i]
// do stuff with rate
}
The enumerate approach looks like this
for (index, rate) in rates.enumerate()
{
print("Do stuff with \(rate) at position \(index)")
}
if you just need each object in turn it's a bit easier
for rate in rates
{
// do stuff with rate
}
Because it's a dictionary, no enumerate() required:
var aDictionary: [String: Float] = ["EUR": 1, "RON": 4.5,
"USD": 1.3]
for (index,item) in aDictionary{
print(index,item)
}