Swift 2 Unused Constant Warning

Swift 2 unused constant warning

You're not replacing let with _, but you're replacing the variable name with it. If the variable isn't used anywhere in the code it's irrelevant so the line can be written like:

_ = someVal["value"]

If you want to use it somewhere you need a name for it to reference it later on. But when you don't use it writing _ is a lot easier ...

Compiler warning for unused immutable sink, but using _ causes test to fail

In the code where you store the AnyCancellable in a local property named sink, the compiler issues the warning that you never used the variable. This means that the compiler is allowed to optimize away the variable and destroy the AnyCancellable immediately.

However, you are probably running your tests in a Debug build configuration. The default settings for a Debug configuration disable optimization. So the compiler does not optimize away the sink variable and only destroys it at the end of the scope. By then, I assume, you have already posted the notification you're expecting.

In the code where you use _, the compiler destroys the returned AnyCancellable immediately. This cancels your subscription to the notification publisher, so the notification is never delivered to your sink closure.

You can silence the warning, and indeed guarantee that the local property's value isn't destroyed until later, using the withExtendedLifetime function:

let sink = deviceConnectedPublisher.sink { _ in
expectation.fulfill()
}

// post notification here

withExtendedLifetime(sink) { }
// Only now can sink be destroyed.

if let warning in Swift 2.0

It means that the code never accessed / used the constant "let a" inside the if- block.

Probably you don't need the if-block at all if you do not need to access "let a". Maybe you used responseString! instead? Or do you only want to execute your code if the responseString is not nil (a simple if responseString != nil would do it then)?

After the if-block, the "let a" a will be marked for garbage collection or marked to be deleted after the if block during compile time (not sure about that).

Thus the compiler thinks that it is a good idea to replace the condition, as the assignment

let a = responseString 

will result in false if

responseString == nil

and true in all other cases.

This condition-assignment is used in 99% of the cases to assure that e.g. the nillable variable responseString is not nil or even more often to automatically unwrap it on-the-fly.

Swift 3 / How to use result of warning Result of call is unused

You might say:

let constraints = imageView.autoLayoutToSuperview()
NSLayoutConstraint.activate(constraints)

Result of call is unused

You are confusing the results variable, which is, indeed, used inside the closure, and the result of the taskForDELETEMethod call itself, which is NSURLSessionDataTask object.

From the examples of using taskForDELETEMethod that I was able to find online it looks like it is perfectly OK to ignore the return value, so you can avoid this warning by assigning the result to _ variable, i.e.

let _ = taskForDELETEMethod {
... // The rest of your code goes here
}

Xcode complains about Unused functions that are used

Defining a static function (or variable, for that matter) in a header file means every source file that imports that header file will get its own copy.

That is not good and is what the compiler is complaining about (not every source file references this function).

Make it static inline instead:

static inline BOOL isIndexValid(NSInteger index) {
return ((index >=0) && (index < 200));
}

What to do with Swift's try? that cause Result of try? is unused?

As your requirement, "At the same time I don't care about the exception that may be thrown, I just want to call method", do this:

try! managedObjectContext.save()

But it will crash if an error is thrown. So, use below code snip for safe:

_ = try? managedObjectContext.save()


Related Topics



Leave a reply



Submit