Swift 4.2 computed variable [String:Bool] does not assign value correctlyWhat does this mean? “'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X”What does an exclamation mark mean in the Swift language?using key : value after loading dictionary from a plist using swiftSwift if statement not working. Clearly a bug, but whats the patterntextView breaking lines automatically - SwiftHow does one assign values to nested [NSObject: AnyObject] dictionaries?Why non optional Any can hold nil?swift for-in loop in playground just counts loops, does not printSwift force cast Bool to Int error when creating key/value pairs in dictionaryNil checks not always working for Any in Swift

You've spoiled/damaged the card

Calling GPL'ed socket server inside Docker?

How to skip replacing first occurrence of a character in each line?

Their answer is discrete, mine is continuous. They baited me into the wrong answer. I have a P Exam question

Is there any word or phrase for negative bearing?

How to make thick Asian sauces?

Incremental Ranges!

Do any instruments not produce overtones?

How were concentration and extermination camp guards recruited?

Why don't B747s start takeoffs with full throttle?

Aligning object in a commutative diagram

Why did Hela need Heimdal's sword?

Through what methods and mechanisms can a multi-material FDM printer operate?

Can't login after removing Flatpak

Completing the square to find if quadratic form is positive definite.

PRBHA-10: A hashing algorithm in Python

Why is the relationship between frequency and pitch exponential?

Bug using breqn and babel

PhD student with mental health issues and bad performance

C SIGINT signal in Linux

Did Darth Vader wear the same suit for 20+ years?

Why don’t airliners have temporary liveries?

What do we gain with higher order logics?

What are the words for people who cause trouble believing they know better?



Swift 4.2 computed variable [String:Bool] does not assign value correctly


What does this mean? “'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X”What does an exclamation mark mean in the Swift language?using key : value after loading dictionary from a plist using swiftSwift if statement not working. Clearly a bug, but whats the patterntextView breaking lines automatically - SwiftHow does one assign values to nested [NSObject: AnyObject] dictionaries?Why non optional Any can hold nil?swift for-in loop in playground just counts loops, does not printSwift force cast Bool to Int error when creating key/value pairs in dictionaryNil checks not always working for Any in Swift






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















[MacOS 10.14.1, Xcode 10.1, Swift 4.2]



I'm working on creating a getopt style CLI argument processor whilst practising Swift. In my design, I decided to create a computed variable, represented as a [String:Bool] dictionary, that can be checked to see if an option (key) is just a switch (value = true) or whether it may include parameters (value = false). So I've written the code below, all of which is, at the moment, in my small (300 lines) main.swift file.



The code works correctly in a playground, but in my Swift Xcode project, whilst the dictionary's keys are correct, values are always false and inconsistent with the printed messages.



let options = "cwt:i:o:"
//lazy var optionIsSwitch : [String:Bool] = { (This will be moved to a class)
var optionIsSwitch : [String:Bool] =
var tmpOptionIsSwitch : [String:Bool] = [:]
let optionsStrAsArray = Array(options)
let flags = Array(options.filter !":".contains($0) )
tmpOptionIsSwitch.reserveCapacity(flags.count)
for thisOption in 0...flags.count-1
var posInOptionsStr = 0
while posInOptionsStr < optionsStrAsArray.count-1 && flags[thisOption] != optionsStrAsArray[posInOptionsStr]
posInOptionsStr += 1

if posInOptionsStr < optionsStrAsArray.count-1 && optionsStrAsArray[posInOptionsStr+1] == ":"
tmpOptionIsSwitch[String(flags[thisOption])] = false
print("(flags[thisOption]) is FALSE")
else
tmpOptionIsSwitch[String(flags[thisOption])] = true
print("(flags[thisOption]) is TRUE")


return tmpOptionIsSwitch
()


I've stepped through the code in my project to observe the execution sequence, and found it to be correct. As per the first image, tmpOptionIsSwitch returns a dictionary containing the right keys but all the values are set to false, which is inconsistent with the print statements.



Xcode Project Debug with print tmpOptionIsSwitch
As part of my debugging activities, I copied the above code into a Swift Playground where I found it gave the correct results, as per the image below.



Playground Results



Has anyone has such an issue? Is there something I've done wrong?










share|improve this question



















  • 1





    The debugger view is sometimes wrong. Does print(tmpOptionIsSwitch) print the expected result?

    – Martin R
    Mar 24 at 15:05











  • It's just a debug error printing. The actual value is correct.

    – E.Coms
    Mar 24 at 15:15











  • I've updated the Xcode project debug image to include print tmpOptionIsSwitch, it is not correct.

    – gone
    Mar 24 at 15:40











  • @gone I can't see the difference. It the same for both command line and playground: Hello, Swift 4.2 c is TRUE w is TRUE t is FALSE i is FALSE o is FALSE ["i": false, "o": false, "c": true, "w": true, "t": false]

    – Vyacheslav
    Mar 24 at 15:46











  • @Vyacheslav, no it isn't the same. value = false in all cases in Xcode. The Xcode print statements only prove the execution path is correct, but the assignments are forgotten somewhere along the way. I'm using the print statements as a debug tool.

    – gone
    Mar 24 at 15:50

















0















[MacOS 10.14.1, Xcode 10.1, Swift 4.2]



I'm working on creating a getopt style CLI argument processor whilst practising Swift. In my design, I decided to create a computed variable, represented as a [String:Bool] dictionary, that can be checked to see if an option (key) is just a switch (value = true) or whether it may include parameters (value = false). So I've written the code below, all of which is, at the moment, in my small (300 lines) main.swift file.



The code works correctly in a playground, but in my Swift Xcode project, whilst the dictionary's keys are correct, values are always false and inconsistent with the printed messages.



let options = "cwt:i:o:"
//lazy var optionIsSwitch : [String:Bool] = { (This will be moved to a class)
var optionIsSwitch : [String:Bool] =
var tmpOptionIsSwitch : [String:Bool] = [:]
let optionsStrAsArray = Array(options)
let flags = Array(options.filter !":".contains($0) )
tmpOptionIsSwitch.reserveCapacity(flags.count)
for thisOption in 0...flags.count-1
var posInOptionsStr = 0
while posInOptionsStr < optionsStrAsArray.count-1 && flags[thisOption] != optionsStrAsArray[posInOptionsStr]
posInOptionsStr += 1

if posInOptionsStr < optionsStrAsArray.count-1 && optionsStrAsArray[posInOptionsStr+1] == ":"
tmpOptionIsSwitch[String(flags[thisOption])] = false
print("(flags[thisOption]) is FALSE")
else
tmpOptionIsSwitch[String(flags[thisOption])] = true
print("(flags[thisOption]) is TRUE")


return tmpOptionIsSwitch
()


I've stepped through the code in my project to observe the execution sequence, and found it to be correct. As per the first image, tmpOptionIsSwitch returns a dictionary containing the right keys but all the values are set to false, which is inconsistent with the print statements.



Xcode Project Debug with print tmpOptionIsSwitch
As part of my debugging activities, I copied the above code into a Swift Playground where I found it gave the correct results, as per the image below.



Playground Results



Has anyone has such an issue? Is there something I've done wrong?










share|improve this question



















  • 1





    The debugger view is sometimes wrong. Does print(tmpOptionIsSwitch) print the expected result?

    – Martin R
    Mar 24 at 15:05











  • It's just a debug error printing. The actual value is correct.

    – E.Coms
    Mar 24 at 15:15











  • I've updated the Xcode project debug image to include print tmpOptionIsSwitch, it is not correct.

    – gone
    Mar 24 at 15:40











  • @gone I can't see the difference. It the same for both command line and playground: Hello, Swift 4.2 c is TRUE w is TRUE t is FALSE i is FALSE o is FALSE ["i": false, "o": false, "c": true, "w": true, "t": false]

    – Vyacheslav
    Mar 24 at 15:46











  • @Vyacheslav, no it isn't the same. value = false in all cases in Xcode. The Xcode print statements only prove the execution path is correct, but the assignments are forgotten somewhere along the way. I'm using the print statements as a debug tool.

    – gone
    Mar 24 at 15:50













0












0








0








[MacOS 10.14.1, Xcode 10.1, Swift 4.2]



I'm working on creating a getopt style CLI argument processor whilst practising Swift. In my design, I decided to create a computed variable, represented as a [String:Bool] dictionary, that can be checked to see if an option (key) is just a switch (value = true) or whether it may include parameters (value = false). So I've written the code below, all of which is, at the moment, in my small (300 lines) main.swift file.



The code works correctly in a playground, but in my Swift Xcode project, whilst the dictionary's keys are correct, values are always false and inconsistent with the printed messages.



let options = "cwt:i:o:"
//lazy var optionIsSwitch : [String:Bool] = { (This will be moved to a class)
var optionIsSwitch : [String:Bool] =
var tmpOptionIsSwitch : [String:Bool] = [:]
let optionsStrAsArray = Array(options)
let flags = Array(options.filter !":".contains($0) )
tmpOptionIsSwitch.reserveCapacity(flags.count)
for thisOption in 0...flags.count-1
var posInOptionsStr = 0
while posInOptionsStr < optionsStrAsArray.count-1 && flags[thisOption] != optionsStrAsArray[posInOptionsStr]
posInOptionsStr += 1

if posInOptionsStr < optionsStrAsArray.count-1 && optionsStrAsArray[posInOptionsStr+1] == ":"
tmpOptionIsSwitch[String(flags[thisOption])] = false
print("(flags[thisOption]) is FALSE")
else
tmpOptionIsSwitch[String(flags[thisOption])] = true
print("(flags[thisOption]) is TRUE")


return tmpOptionIsSwitch
()


I've stepped through the code in my project to observe the execution sequence, and found it to be correct. As per the first image, tmpOptionIsSwitch returns a dictionary containing the right keys but all the values are set to false, which is inconsistent with the print statements.



Xcode Project Debug with print tmpOptionIsSwitch
As part of my debugging activities, I copied the above code into a Swift Playground where I found it gave the correct results, as per the image below.



Playground Results



Has anyone has such an issue? Is there something I've done wrong?










share|improve this question
















[MacOS 10.14.1, Xcode 10.1, Swift 4.2]



I'm working on creating a getopt style CLI argument processor whilst practising Swift. In my design, I decided to create a computed variable, represented as a [String:Bool] dictionary, that can be checked to see if an option (key) is just a switch (value = true) or whether it may include parameters (value = false). So I've written the code below, all of which is, at the moment, in my small (300 lines) main.swift file.



The code works correctly in a playground, but in my Swift Xcode project, whilst the dictionary's keys are correct, values are always false and inconsistent with the printed messages.



let options = "cwt:i:o:"
//lazy var optionIsSwitch : [String:Bool] = { (This will be moved to a class)
var optionIsSwitch : [String:Bool] =
var tmpOptionIsSwitch : [String:Bool] = [:]
let optionsStrAsArray = Array(options)
let flags = Array(options.filter !":".contains($0) )
tmpOptionIsSwitch.reserveCapacity(flags.count)
for thisOption in 0...flags.count-1
var posInOptionsStr = 0
while posInOptionsStr < optionsStrAsArray.count-1 && flags[thisOption] != optionsStrAsArray[posInOptionsStr]
posInOptionsStr += 1

if posInOptionsStr < optionsStrAsArray.count-1 && optionsStrAsArray[posInOptionsStr+1] == ":"
tmpOptionIsSwitch[String(flags[thisOption])] = false
print("(flags[thisOption]) is FALSE")
else
tmpOptionIsSwitch[String(flags[thisOption])] = true
print("(flags[thisOption]) is TRUE")


return tmpOptionIsSwitch
()


I've stepped through the code in my project to observe the execution sequence, and found it to be correct. As per the first image, tmpOptionIsSwitch returns a dictionary containing the right keys but all the values are set to false, which is inconsistent with the print statements.



Xcode Project Debug with print tmpOptionIsSwitch
As part of my debugging activities, I copied the above code into a Swift Playground where I found it gave the correct results, as per the image below.



Playground Results



Has anyone has such an issue? Is there something I've done wrong?







swift xcode macos command-line-interface






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 15:39







gone

















asked Mar 24 at 14:46









gonegone

4331820




4331820







  • 1





    The debugger view is sometimes wrong. Does print(tmpOptionIsSwitch) print the expected result?

    – Martin R
    Mar 24 at 15:05











  • It's just a debug error printing. The actual value is correct.

    – E.Coms
    Mar 24 at 15:15











  • I've updated the Xcode project debug image to include print tmpOptionIsSwitch, it is not correct.

    – gone
    Mar 24 at 15:40











  • @gone I can't see the difference. It the same for both command line and playground: Hello, Swift 4.2 c is TRUE w is TRUE t is FALSE i is FALSE o is FALSE ["i": false, "o": false, "c": true, "w": true, "t": false]

    – Vyacheslav
    Mar 24 at 15:46











  • @Vyacheslav, no it isn't the same. value = false in all cases in Xcode. The Xcode print statements only prove the execution path is correct, but the assignments are forgotten somewhere along the way. I'm using the print statements as a debug tool.

    – gone
    Mar 24 at 15:50












  • 1





    The debugger view is sometimes wrong. Does print(tmpOptionIsSwitch) print the expected result?

    – Martin R
    Mar 24 at 15:05











  • It's just a debug error printing. The actual value is correct.

    – E.Coms
    Mar 24 at 15:15











  • I've updated the Xcode project debug image to include print tmpOptionIsSwitch, it is not correct.

    – gone
    Mar 24 at 15:40











  • @gone I can't see the difference. It the same for both command line and playground: Hello, Swift 4.2 c is TRUE w is TRUE t is FALSE i is FALSE o is FALSE ["i": false, "o": false, "c": true, "w": true, "t": false]

    – Vyacheslav
    Mar 24 at 15:46











  • @Vyacheslav, no it isn't the same. value = false in all cases in Xcode. The Xcode print statements only prove the execution path is correct, but the assignments are forgotten somewhere along the way. I'm using the print statements as a debug tool.

    – gone
    Mar 24 at 15:50







1




1





The debugger view is sometimes wrong. Does print(tmpOptionIsSwitch) print the expected result?

– Martin R
Mar 24 at 15:05





The debugger view is sometimes wrong. Does print(tmpOptionIsSwitch) print the expected result?

– Martin R
Mar 24 at 15:05













It's just a debug error printing. The actual value is correct.

– E.Coms
Mar 24 at 15:15





It's just a debug error printing. The actual value is correct.

– E.Coms
Mar 24 at 15:15













I've updated the Xcode project debug image to include print tmpOptionIsSwitch, it is not correct.

– gone
Mar 24 at 15:40





I've updated the Xcode project debug image to include print tmpOptionIsSwitch, it is not correct.

– gone
Mar 24 at 15:40













@gone I can't see the difference. It the same for both command line and playground: Hello, Swift 4.2 c is TRUE w is TRUE t is FALSE i is FALSE o is FALSE ["i": false, "o": false, "c": true, "w": true, "t": false]

– Vyacheslav
Mar 24 at 15:46





@gone I can't see the difference. It the same for both command line and playground: Hello, Swift 4.2 c is TRUE w is TRUE t is FALSE i is FALSE o is FALSE ["i": false, "o": false, "c": true, "w": true, "t": false]

– Vyacheslav
Mar 24 at 15:46













@Vyacheslav, no it isn't the same. value = false in all cases in Xcode. The Xcode print statements only prove the execution path is correct, but the assignments are forgotten somewhere along the way. I'm using the print statements as a debug tool.

– gone
Mar 24 at 15:50





@Vyacheslav, no it isn't the same. value = false in all cases in Xcode. The Xcode print statements only prove the execution path is correct, but the assignments are forgotten somewhere along the way. I'm using the print statements as a debug tool.

– gone
Mar 24 at 15:50












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55324979%2fswift-4-2-computed-variable-stringbool-does-not-assign-value-correctly%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55324979%2fswift-4-2-computed-variable-stringbool-does-not-assign-value-correctly%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript