ServerShuttingDown is not a valid member of ServerStorage "ServerStorage" - roblox

I found an error saying [ServerShuttingDown is not a valid member of ServerStorage "ServerStorage"] on this line of code:
game.ServerStorage.ServerShuttingDown = false
ServerShuttingDown is a booleanvalue in ServerStorage.

Any Value object, whether it's an IntValue, BoolValue, StringValue, etc., they all have a Value property. If you want to update it, you need to set that value.
local boolValue = game.ServerStorage.ServerShuttingDown
boolValue.Value = false

Related

Comparing non-optional value of type 'Bool' to 'nil' always returns true

I have an if-else statement where I am checking if the value coming from user defaults is nil or not like this:
if defaults.bool(forKey: "abcd") != nil{
//Do something
}
else{
//do something else
}
But Xcode is giving me an error saying:
"Comparing non-optional value of type 'Bool' to 'nil' always returns true"
Can someone explain what's happening here and how to fix this?
bool(forKey:) returns a NON-optional, which cannot be nil. If the key is missing in the user defaults, the return value will be false.
If you want trinary logic here (nil/true/false) use object(forKey:) looking for an NSNumber, and if present, take its boolValue.
As
defaults.bool(forKey: "abcd")
will return false by default check Docs , so it will never be optional
The Boolean value associated with the specified key. If the specified key doesn‘t exist, this method returns false.
The func bool(forKey: "abcd") returns Bool type not optional.
Which means you cant compare it to bool, what you can do is simply:
if defaults.bool(forKey: "abcd") {
//Do something
} else {
//do something else
}
Now if the key exists and has true value it will get into the if statement, if it does not exists or is false it will go to the else.
If you have any doubts you can read about the func in the following Apple developer link: Apple:bool(forKey:)
Objective-c property in swift. If you're using some objective c property in swift and it says something like "Comparing non-optional value of type 'XYZ' to 'nil' always returns true" you have to make that objective c property to "_Nullable" so that property may not be optional anymore. Like #property (strong,nonatomic) NSString *_Nullable someString;
defaults.bool(forKey: "abcd") != nil
The first part, defaults.bool(forKey: "abcd"), returns a non-optional boolean. We know that because bool(forKey:) returns Bool, not Bool?. Therefore, you'll always get a Bool value, i.e. either true or false, never nil. Note the documentation:
If the specified key doesn‘t exist, this method returns false.
"Comparing non-optional value of type 'Bool' to 'nil' always returns true"
The compiler is simply pointing out that it knows that defaults.bool(forKey: "abcd") can't be nil, and since you're comparing it to nil, you're probably making a mistake. Your else block will never execute.
Can someone explain what's happening here and how to fix this?
It depends on what you mean for the code to do. If you want to take different actions depending on whether the value is true or false, then compare it to one of those values. If you want to get an optional value back, use object(forKey:) (which returns an optional) instead.
i solved like this:
if(Userdefaults.standart.bool(forkey: "blablabool"){
}
This works..
When you call this if its null it returns false.

Why does Xcode think this is an optional?

I am declaring a constant in this line of code but if I don't put the `! after it, xXcode gives an error saying:
value of optional type string must be unwrapped.
Why does Xcode think this is an optional? I am just declaring a constant of type String and assigning it to a key that the user will set in the setting section.
Maybe because I am using the UserDefault settings and it's not set yet? If so, how do I get around that?
let jbEmail: String = userDefaults.string(forKey: "JBemail_preference")!
Look at the documentation for UserDefaults string(forKey:). It has a return type of String?. It returns an optional because there might not be a value for the given key.
So your attempt to assign a String? to a String results in the error. The forced unwrap (adding !) resolves the error but it is the worst possible solution because now your app will crash if there is no value for the key.
You should properly handle the situation where there is no value for the key in UserDefaults.
You can assign a default value:
let jbEmail = userDefaults.string(forKey: "JBemail_preference") ?? "Some Default"
Or you can conditional deal with there being no value:
if let jbEmail = userDefaults.string(forKey: "JBemail_preference") {
// Do something with jbEmail
} else {
// There is no value, do something else
}

Unexpectedly found nil while unwrapping an Optional value but value exist

I know I need to bind my varaible for unwrap but the problem is my value is not reconized but present.
This is my code :
surveyW.karmaWin = Int(endedSurvey["karma"].string!)
endedSurvey is a array dictionary of my JSON backend. I get a Unexpectedly found nil while unwrapping an Optional value error.
I specify that I force the unwrapping to show you my problem.
The problem is my array contains the karma value. I show you the screen of the value:
So we can see that the value existing. Why I get a Unexpectedly found nil...?
The value contained in "karma" is not String. You're trying to force cast it with SwiftyJSON but it tells you it has a nil. You first need to extract value as it is - .int, and after that convert that to something else if needed.
surveyW.karmaWin = endedSurvey["karma"].int
You can use intValue because SwiftyJSON has two kinds of "getters" for retrieving values: Optional and non-Optional
.string and .int are the optional getters for the String and Int representation of a value, so you have to unwrap it before use
if let fbId = fbJson["id"].string {
print(fbId)
}
If you are 100% sure that there will always be a value, you can use the equivalent of "force unwrap" by using the non-Optional getter and you don't need if let anymore:
let fbId = fbJson["id"].stringValue
In your code :
surveyW.karmaWin = endedSurvey["karma"].intValue
endedSurvey["karma"] is an Integer not string and also good way to unwrap an optional is:
if let karma = endedSurvey["karma"] as? Int{
surveyW.karmaWin = karma
}

Difference of ? and ! when using an instance of optional type?

var neilPeart: Drummer? = Drummer()
var rush: Band? = Band(drummer: neilPaert!)
Suppose we have an instance named neilPeart of optional type of Drummer, and an instance named rush of optional type of Band. There is a band property for Drummer instances. If I want to change the band property of neilPeart, what's the difference between "neilPeart?.band = rush" and "neilPeart!.band = rush"
Optionals use ? marks which means that if I were to say var title: String?. That would mean that title could either contain a value or be nil. The ! points are used to unwrap an optional value. This could create an issue for you and give you an error if your value is nil. The error would result in not being able to unwrap a nil object. Hope this helps!
? will set it to nil if there is no value
! will force unwrap the value and will crash the app if there is no value

Why my optional value is not unwrapped, Swift?

I have pretty simple example and have no clue why it doesn't work as expected.
var list:Array<Int> = [1,2,3,4,5]
var item:Int?
for var index = 0; index < list.count; index++ {
item! = list[index]
item = item + 5 // <-- error value of optional type 'Int?' not unwrapped
}
Why Swift forces me to write: item = item! + 5
I unwrapped it here: item! = list[index] and if list returns nil - the Exception will be thrown.
As I understand on this step a.e.: item! = list[index] the item is not nil
I tried several options like:
item! = list[index] as Int!
item! = list[index] as AnyOblect! as? Int
But still get the same demand to write item = item! + 5
I use playground
Let me break it down for you:
var item:Int? tells the compiler that whenever the word item is used, it refers to a variable of type optional Int (aka Int?).
var item:Int on the other hand, tells the compiler that whenever the word item is used, it refers to a variable simply of type Int.
In order to access the unwrapped value of your variable declared in var item:Int?, you will always have to use item!. The compiler is not going to guess whether the variable item has a value or not. That, after all, is the the whole purpose of optionals. To make it clear that these kind of variables may or may not have a value.
Essentially, what I'm trying to say is that a variable once declared as an optional, will always be an optional regardless of whether it contains a value or not. To get the unwrapped value, you will always have to use the character !, unless you decide to store it's value in another variable (ex. var unwrappedItem = item!)
To get rid of your error, simply declare your item variable to be of type Int, and not Int?.
As to why Swift throws an error instead of just letting the runtime raise an exception, it's just an extra precaution to probably discourage people from using bad practice.