"Initialisation of Immutable Value'Context' Was Never Used, Consider Replacing Assignment to '_' or Removing It

Swift; Initialization of immutable value was never used

If you want it to change it, you should be making it a variable and using the same variable.

var message = "This is a test"

if message.range(of:"test") != nil {
message = "Changed string"
}

print(message)

Initialisation of immutable value'context' was never used, consider replacing assignment to '_' or removing it

It's all in the error message

value...was never used

Your variable isn't being used anywhere, so Xcode tells you that you can remove it (because having unused variables is a waste of memory). Just use your variable somewhere and the error will go away (e.g. get a value from it, print it, etc).

Of course you mean to use it somewhere right? Otherwise you wouldn't have declared it. It's just that the Xcode (especially the new one, I noticed) checks for errors immediately, so these kinds of errors appear before you can really do anything about it.

Edit: I didn't imagine that almost 7 years later people would still be commenting on a post regarding a beginner-level compiler warning, but to be more specific, yes there are some cases where you might not want to keep a variable around or where you want to discard some return value, so you don't necessarily always "mean to use it somewhere". See comments for more.

Initialization of variable was never used

If you plan to use this variables later - just ignore this warnings.

But didn't you mean something like this?

var allText = ""
var firstLetter = ""
var firstSentence = ""
var body = ""
var info = ""
var map = ""
var about = ""

func changeNameToIndex(index: Int) {
switch index {
case 0:
allText = "..."
firstLetter = "..."
// etc
case 1:
allText = "..."
firstLetter = "..."
// etc
case 2:
// ....
default:
// ....
}
}

Calling self-defined closure in Swift

There isn't anything wrong with your declaration. But as the word tells you, you are just declaring something and not calling. So the closure is not called anywhere.

Try this one

typealias myBlock = (param1:AnyObject, param2:AnyObject, param3:Bool) -> ()

let block: myBlock = {(param1,param2,param3) in
if (param3){
print(param1,param2)
}}

block(param1: 1,param2: 2,param3: true)

You have to replace the actual parameter with your values of course. Works like a charm as you can see in the playground screenshot.

Sample Image



Related Topics



Leave a reply



Submit