SWIFT – Check if it is zero, forcibly open the options for security

I have a network method that returns a completion closure and takes Error as a parameter.

I usually avoid forced unpacking, but in this In this case, I use a guard statement to check if the error is nil:

guard error == nil else {
print(error!)
return< br />}

...

The method I am calling is from the SDK, so I really don’t know what is going on there.

General situation Next, is this force safe to unlock threads?

The difference between if and guard is simple, but in your case, you should use if, not guard. Guard should be used when certain values ​​are expected to exist to make the function perform as expected. You don’t want an error, but if it is not nil, you must return. So you should use if let:

< /p>

if let error = error {
print(error)
return
}

In this way, you don’t need to use Forced unwrapping, it can improve the way the code is expressed.

Back to your question “In general, is this force unlocking threads safe?”, assuming you are really talking about thread safety, That must be thread-safe. Closure variables are immutable.

If you are talking about safety (that is, “fatal error: unexpectedly found to be zero when expanding optional values”), then It must also be safe. Before opening it, the error must not be zero (because of protection), so the deployment force is safe.

In short, your current code is bulletproof, but use if let It might be better.

I have a network method that returns a completion closure and takes Error as a parameter.

I usually Avoid forced unpacking, but in this case, I use a guard statement to check if the error is nil:

guard error == nil else {
print( error!)
return
)

...

The method I’m calling is from the SDK, so I don’t really know what’s going on there.

Under normal circumstances, is it safe to unlock threads by this force?

The difference between if and guard is simple, but in your case, you should use if instead of guard. When certain values ​​are expected to exist To make the function execute as expected, guard should be used. You do not want an error, but if it is not nil, you must return. So you should use if let:

if let error = error {
print(error)
return
}

In this way, you don’t need to use forced unwrapping, it can improve the way the code is expressed.

Go back to your question “In general, is it safe to unlock threads by this force?” If you are really talking about thread safety, it must be thread safe. Closure variables are not allowed. Change.

If you are talking about security (that is, “fatal error: accidentally found to be zero when expanding optional values”), then it must also be safe. Before opening it, The error must not be zero (because of protection), so the expansion force is safe.

In short, your current code is bulletproof, but using if let may be better.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4117 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.