got error on xcode7. view.layer.renderInContext(context!) - swift

The error said that "Value of optional type 'CGContext?' not unwrapped; did you mean to use '!' or '?'?".
Does anyone know how to fix this??
view.layer.renderInContext(context!)

If that line is giving you that error, context had to be of type CGContext??.
You can, as the error says, use ! or ? to unwrap the optional resulting from the expression context!.

Related

How to use strings for OSLogMessage?

Given the following two lines, XCode is showing an error for the second line saying Cannot convert value of type 'String' to expected argument type 'OSLogMessage'.
logger.info("Device found:")
logger.info(String(describing: device))
Can someone please explain why this error is shown? In both cases, the parameter is of type String. (I guess)
Currently, I fix this by using string interpolation. But this does not feel right. Is there a better way than:
logger.info("\(String(describing: device))")

! casts the expression to its underlying non-nullable type in Flutter

What does it mean when they say:
A postfix exclamation mark (!) takes the expression on the left and casts it to its underlying non-nullable type.
https://dart.dev/null-safety/understanding-null-safety#null-assertion-operator
Does it mean that it checks whether the expression is null or not and then throws error before executing anything further?
Please explain this:
casts it to its underlying non-nullable type
Does it mean that it checks whether the expression is null or not and then throws error before executing anything further?
Yes, ! will insert a null-check that will throw a TypeError if the expression turns out to be null at runtime.
As stated in the last paragraph of the "Null assertion operator" section:
Of course, like any cast, using ! comes with a loss of static safety. The cast must be checked at runtime to preserve soundness and it may fail and throw an exception.
casts it to its underlying non-nullable type
It means that the variable might be nullable but when you are explicitly sure that the current call to the variable will not return null then you use the ! operator to imply that the variable is not null.
For example you have this:
List<ChatMessage?> messages;
This would be a list of messages. But they can be null.
The message has a body, id etc.
If I would try to call an id on a message that is inside the list as followed:
message.id
Message can be null in this case which will tell me that Message cannot be Null when calling id.
So what we do here is a ! to the end of message then it casts to its underlying type which can't be a null at this point(because we define it cannot be with the !) or it will throw an error:
messages!.id

How can you get the default error string used when an optional-nil error is thrown?

We're implementing our own version of the .required() method on optionals where instead of simply force-unwrapping throwing an error, you get the file, function and line number of where the offense took place as well as a dev-supplied message of what went wrong.
The message itself is optional, so if the user doesn't specify it, we want ours to share the same message as Swift's default exception for force-unwrapping a nil optional. Their default text is this...
Unexpectedly found nil while unwrapping an Optional value
When not showing our custom message, we of course want ours to show the same text for log analysis. While we can simply hard-code the above string, I was wondering if there's a way to extract it from the built-in error. Something like this...
let ourMsg = UnexpectedNilError.localizedDescription
However, not sure what goes in place of UnexpectedNilError above, or if this is even possible. Not that big a deal. Just wondering if there are standard errors we can tap into here.
After a quick search on Swift's repo, the error message is found in the file Optional.swift line 314:
_preconditionFailure(
"Unexpectedly found nil while unwrapping an Optional value",
file: StaticString(_start: _filenameStart,
utf8CodeUnitCount: _filenameLength,
isASCII: _filenameIsASCII),
line: UInt(_line))
It seems to be quite hardcoded as well, being passed directly as a parameter to _preconditionFailure, so it doesn't seem like you can get it as a string value in your code.

FIRAuth.auth() compiling error: "Cannot use optional chaining on non optional value 'FIRAuth'"

Cannot use optional chaining on non optional value 'FIRAuth'
I have tested every solutions, but always got the same error.
Even if i create a new project, when i'm using FIRAuth, i always got a compiling error.
Can someone help me please. I use Swift 2, Xcode 7, IOS9
If you are trying to add FIRAuth.auth()? try to remove (?).
FIRAuth.auth() is non optional so treating them as one might result to the error “Cannot use optional chaining on non optional value 'FIRAuth'”
Optional chaining is a process for querying and calling properties,
methods, and subscripts on an optional that might currently be nil
Check Optional Chaining

What the difference between using or not using "!" in Swift?

The ! mark is used for the optional value in Swift to extract the wrapped value of it, but the value can be extracted without the !:
var optionalString: String? = "optionalString"
println(optionalString)
another example using the !:
var optionalString: String? = "optionalString"
println(optionalString!)
both the codes above get the right value, so I wonder what's the difference between using and not using !, is it just for detect an error at runtime if the optional value is nil or something else? Thanks in advance.
As others have already stated, a really good place to start would be with the official documentation. This topic is extraordinarily well covered by the documentation.
From the official documentation:
Trying to use ! to access a non-existent optional value triggers a
runtime error. Always make sure that an optional contains a non-nil
value before using ! to force-unwrap its value.
println() is probably not the best way to test how the ! operator works. Without it, println() will either print the value or nil, with it it will either print the value or crash.
The main difference is when we're trying to assign our optional to another value or use it in an argument to a function.
Assume optionalValue is an optional integer.
let actualValue = optionalValue
Using this assignment, actualValue is now simply another optional integer. We haven't unwrapped the value at all. We have an optional rather than an integer.
Meanwhile,
let actualValue = optionalValue!
Now we're forcing the unwrap. Actual value will be an integer rather than an optional integer. However, this code will cause a runtime exception is optionalValue is nil.
Your question is answered in the book "The Swift Programming Language: iBooks
Download it and search it for "!".
You could've easily found the answer without having to ask here. For future reference, please always remember to look in the manual first.
From "The Swift Programming Language":
You can use an "if" statement to find out whether an optional contains a value. If an optional does have a value, it evaluates to "true"; if it has no value at all, it evaluates to "false".
Once you're sure that the optional does contain a value, you can access its underlying value by adding an exclamation mark (!) to the end of the optional's name. The exclamation mark effectively says, "I know that this optional definitely has a value; please use it." This is known as forced unwrapping of the optional's value.
I've never heard of Swift before now, but reading the documentation seems clear
"Using the ! operator to unwrap an optional that has a value of nil results in a runtime error."
If in your application nil is a valid and expected value in any normal circumstance and/or you want to trap and handle it in your code then I suggest that you don't use it. If you never expect it to be nil then use it as a debug aid. Runtime error suggest to me that your program will be terminated with a message reported by the OS.