Default dictionary value in SwiftHow to call Objective-C code from Swift#pragma mark in Swift?Swift for loop: for index, element in array?Swift Beta performance: sorting arraysRemoving duplicate elements from an array in SwiftSorting Dictionaries containing more complex values in SwiftSwift Dictionary Multiple Key Value Pairs - IterationDoes assigning a property of a tuple contained in a Swift Dictionary change the Dictionary itself?Default value of uninitialized variable/object in SwiftConvert JSON String to Swift Dictionary

Using Forstner bits instead of hole saws

Generate random number in Unity without class ambiguity

Is it uncompelling to continue the story with lower stakes?

In a KP-K endgame, if the enemy king is in front of the pawn, is it always a draw?

Went to a big 4 but got fired for underperformance in a year recently - Now every one thinks I'm pro - How to balance expectations?

Accurately recalling the key - can everyone do it?

Who's behind community AMIs on Amazon EC2?

Can there be multiple energy eigenstates corresponding to the same eigenvalue of a Hamiltonian (Pauli-X)?

Is there a word that describes people who are extraverted and/or energetic, but uneducated, unintelligent and/or uncreative?

Can I say "Gesundheit" if someone is coughing?

Have you been refused entry into the Federal Republic of Germany?

How does Rust's 128-bit integer `i128` work on a 64-bit system?

Is the first page of Novel really that important?

A wiild aanimal, a cardinal direction, or a place by the water

Empty proof as standalone

Why did the United States not resort to nuclear weapons in Vietnam?

In-Cabinet (sink base) electrical box - Metal or Plastic?

Why is the Vasa Museum in Stockholm so Popular?

Why does BezierFunction not follow BezierCurve at npts>4?

How does shared_ptr<void> know which destructor to use?

Is Norway in the Single Market?

Feedback diagram

Deflecting lasers with lightsabers

Different answers of calculations in LuaLaTeX on local computer, lua compiler and on overleaf



Default dictionary value in Swift


How to call Objective-C code from Swift#pragma mark in Swift?Swift for loop: for index, element in array?Swift Beta performance: sorting arraysRemoving duplicate elements from an array in SwiftSorting Dictionaries containing more complex values in SwiftSwift Dictionary Multiple Key Value Pairs - IterationDoes assigning a property of a tuple contained in a Swift Dictionary change the Dictionary itself?Default value of uninitialized variable/object in SwiftConvert JSON String to Swift Dictionary






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I know I can have a default dictionary value in swift, but I am struggling to do this for a tuple



i.e.



var freq = [Int:(Int,Int,Int)]()


I want to make a default value



freq[num.element, default: (0, num.offset, num.offset) ] = (7, 0, 0 )


This produces the frequency table with 7,0,0 for every value when the key does not exist.



Can I use the default for a more complicated dictionary?



For an example if we have an array of numbers [1,2,2,3,3,3]
we can count the number of elements using a frequency table



var freq = [Int:Int]()
for num in nums
freq[num, default: 0] += 1



We want to store the initial position of each number, and the final position of each number in the frequency table so use



var freq = [Int:(Int,Int,Int)]()
for num in nums.enumerated()
if freq[num.element] == nil
freq[num.element] = (1,num.offset, num.offset)
else
freq[num.element] = (freq[num.element]!.0 + 1, freq[num.element]!.1, num.offset)




For this code I want to use a default value.



I am looking for a way of using a default value on a frequency list containing more than just a single value. It is not relevant that I am using a tuple rather than an array of values, for an example I want to find out how to use default with a tuple using arrays.



I tried to use the example above to make a default value, and on testing it does not work. I have looked at previous questions, a Google Search and looked at Apple's documentation.



Question: How to use default for a dictionary var freq = Int:(Int,Int,Int)










share|improve this question


























  • The default value—which is (0, num.offset, num.offset)—is never used in the code in your questions. In other words: the code in your question could be written as freq[num.element] = (7, 0, 0).

    – David Rönnqvist
    Mar 27 at 2:05












  • It's a frequency table. So traverse a String and count the number of each element. Whether this is used or not, the default is not set.

    – WishIHadThreeGuns
    Mar 27 at 2:11







  • 1





    Please show the rest of the code that calculates the frequencies. Currently I find it hard to understand what you're asking without the surrounding context.

    – David Rönnqvist
    Mar 27 at 2:16











  • It's hugely problematic here, as the minimum requirement is clearly stated. I've given an extended example, but frequently doing so on Stack Overflow gives accusations of "homework" (It isn't) or answering a different question unrelated to the one asked (use an array of values, not a tuple! etc.). This one is wholly about the use of defaults with dictionaries in Swift.

    – WishIHadThreeGuns
    Mar 27 at 2:24











  • For what it's worth it's easier to understand what you're asking and what you're trying to achieve after you edited your question to include additional information.

    – David Rönnqvist
    Mar 27 at 3:00

















0















I know I can have a default dictionary value in swift, but I am struggling to do this for a tuple



i.e.



var freq = [Int:(Int,Int,Int)]()


I want to make a default value



freq[num.element, default: (0, num.offset, num.offset) ] = (7, 0, 0 )


This produces the frequency table with 7,0,0 for every value when the key does not exist.



Can I use the default for a more complicated dictionary?



For an example if we have an array of numbers [1,2,2,3,3,3]
we can count the number of elements using a frequency table



var freq = [Int:Int]()
for num in nums
freq[num, default: 0] += 1



We want to store the initial position of each number, and the final position of each number in the frequency table so use



var freq = [Int:(Int,Int,Int)]()
for num in nums.enumerated()
if freq[num.element] == nil
freq[num.element] = (1,num.offset, num.offset)
else
freq[num.element] = (freq[num.element]!.0 + 1, freq[num.element]!.1, num.offset)




For this code I want to use a default value.



I am looking for a way of using a default value on a frequency list containing more than just a single value. It is not relevant that I am using a tuple rather than an array of values, for an example I want to find out how to use default with a tuple using arrays.



I tried to use the example above to make a default value, and on testing it does not work. I have looked at previous questions, a Google Search and looked at Apple's documentation.



Question: How to use default for a dictionary var freq = Int:(Int,Int,Int)










share|improve this question


























  • The default value—which is (0, num.offset, num.offset)—is never used in the code in your questions. In other words: the code in your question could be written as freq[num.element] = (7, 0, 0).

    – David Rönnqvist
    Mar 27 at 2:05












  • It's a frequency table. So traverse a String and count the number of each element. Whether this is used or not, the default is not set.

    – WishIHadThreeGuns
    Mar 27 at 2:11







  • 1





    Please show the rest of the code that calculates the frequencies. Currently I find it hard to understand what you're asking without the surrounding context.

    – David Rönnqvist
    Mar 27 at 2:16











  • It's hugely problematic here, as the minimum requirement is clearly stated. I've given an extended example, but frequently doing so on Stack Overflow gives accusations of "homework" (It isn't) or answering a different question unrelated to the one asked (use an array of values, not a tuple! etc.). This one is wholly about the use of defaults with dictionaries in Swift.

    – WishIHadThreeGuns
    Mar 27 at 2:24











  • For what it's worth it's easier to understand what you're asking and what you're trying to achieve after you edited your question to include additional information.

    – David Rönnqvist
    Mar 27 at 3:00













0












0








0








I know I can have a default dictionary value in swift, but I am struggling to do this for a tuple



i.e.



var freq = [Int:(Int,Int,Int)]()


I want to make a default value



freq[num.element, default: (0, num.offset, num.offset) ] = (7, 0, 0 )


This produces the frequency table with 7,0,0 for every value when the key does not exist.



Can I use the default for a more complicated dictionary?



For an example if we have an array of numbers [1,2,2,3,3,3]
we can count the number of elements using a frequency table



var freq = [Int:Int]()
for num in nums
freq[num, default: 0] += 1



We want to store the initial position of each number, and the final position of each number in the frequency table so use



var freq = [Int:(Int,Int,Int)]()
for num in nums.enumerated()
if freq[num.element] == nil
freq[num.element] = (1,num.offset, num.offset)
else
freq[num.element] = (freq[num.element]!.0 + 1, freq[num.element]!.1, num.offset)




For this code I want to use a default value.



I am looking for a way of using a default value on a frequency list containing more than just a single value. It is not relevant that I am using a tuple rather than an array of values, for an example I want to find out how to use default with a tuple using arrays.



I tried to use the example above to make a default value, and on testing it does not work. I have looked at previous questions, a Google Search and looked at Apple's documentation.



Question: How to use default for a dictionary var freq = Int:(Int,Int,Int)










share|improve this question
















I know I can have a default dictionary value in swift, but I am struggling to do this for a tuple



i.e.



var freq = [Int:(Int,Int,Int)]()


I want to make a default value



freq[num.element, default: (0, num.offset, num.offset) ] = (7, 0, 0 )


This produces the frequency table with 7,0,0 for every value when the key does not exist.



Can I use the default for a more complicated dictionary?



For an example if we have an array of numbers [1,2,2,3,3,3]
we can count the number of elements using a frequency table



var freq = [Int:Int]()
for num in nums
freq[num, default: 0] += 1



We want to store the initial position of each number, and the final position of each number in the frequency table so use



var freq = [Int:(Int,Int,Int)]()
for num in nums.enumerated()
if freq[num.element] == nil
freq[num.element] = (1,num.offset, num.offset)
else
freq[num.element] = (freq[num.element]!.0 + 1, freq[num.element]!.1, num.offset)




For this code I want to use a default value.



I am looking for a way of using a default value on a frequency list containing more than just a single value. It is not relevant that I am using a tuple rather than an array of values, for an example I want to find out how to use default with a tuple using arrays.



I tried to use the example above to make a default value, and on testing it does not work. I have looked at previous questions, a Google Search and looked at Apple's documentation.



Question: How to use default for a dictionary var freq = Int:(Int,Int,Int)







swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 3:09







WishIHadThreeGuns

















asked Mar 27 at 1:31









WishIHadThreeGunsWishIHadThreeGuns

9711 bronze badges




9711 bronze badges















  • The default value—which is (0, num.offset, num.offset)—is never used in the code in your questions. In other words: the code in your question could be written as freq[num.element] = (7, 0, 0).

    – David Rönnqvist
    Mar 27 at 2:05












  • It's a frequency table. So traverse a String and count the number of each element. Whether this is used or not, the default is not set.

    – WishIHadThreeGuns
    Mar 27 at 2:11







  • 1





    Please show the rest of the code that calculates the frequencies. Currently I find it hard to understand what you're asking without the surrounding context.

    – David Rönnqvist
    Mar 27 at 2:16











  • It's hugely problematic here, as the minimum requirement is clearly stated. I've given an extended example, but frequently doing so on Stack Overflow gives accusations of "homework" (It isn't) or answering a different question unrelated to the one asked (use an array of values, not a tuple! etc.). This one is wholly about the use of defaults with dictionaries in Swift.

    – WishIHadThreeGuns
    Mar 27 at 2:24











  • For what it's worth it's easier to understand what you're asking and what you're trying to achieve after you edited your question to include additional information.

    – David Rönnqvist
    Mar 27 at 3:00

















  • The default value—which is (0, num.offset, num.offset)—is never used in the code in your questions. In other words: the code in your question could be written as freq[num.element] = (7, 0, 0).

    – David Rönnqvist
    Mar 27 at 2:05












  • It's a frequency table. So traverse a String and count the number of each element. Whether this is used or not, the default is not set.

    – WishIHadThreeGuns
    Mar 27 at 2:11







  • 1





    Please show the rest of the code that calculates the frequencies. Currently I find it hard to understand what you're asking without the surrounding context.

    – David Rönnqvist
    Mar 27 at 2:16











  • It's hugely problematic here, as the minimum requirement is clearly stated. I've given an extended example, but frequently doing so on Stack Overflow gives accusations of "homework" (It isn't) or answering a different question unrelated to the one asked (use an array of values, not a tuple! etc.). This one is wholly about the use of defaults with dictionaries in Swift.

    – WishIHadThreeGuns
    Mar 27 at 2:24











  • For what it's worth it's easier to understand what you're asking and what you're trying to achieve after you edited your question to include additional information.

    – David Rönnqvist
    Mar 27 at 3:00
















The default value—which is (0, num.offset, num.offset)—is never used in the code in your questions. In other words: the code in your question could be written as freq[num.element] = (7, 0, 0).

– David Rönnqvist
Mar 27 at 2:05






The default value—which is (0, num.offset, num.offset)—is never used in the code in your questions. In other words: the code in your question could be written as freq[num.element] = (7, 0, 0).

– David Rönnqvist
Mar 27 at 2:05














It's a frequency table. So traverse a String and count the number of each element. Whether this is used or not, the default is not set.

– WishIHadThreeGuns
Mar 27 at 2:11






It's a frequency table. So traverse a String and count the number of each element. Whether this is used or not, the default is not set.

– WishIHadThreeGuns
Mar 27 at 2:11





1




1





Please show the rest of the code that calculates the frequencies. Currently I find it hard to understand what you're asking without the surrounding context.

– David Rönnqvist
Mar 27 at 2:16





Please show the rest of the code that calculates the frequencies. Currently I find it hard to understand what you're asking without the surrounding context.

– David Rönnqvist
Mar 27 at 2:16













It's hugely problematic here, as the minimum requirement is clearly stated. I've given an extended example, but frequently doing so on Stack Overflow gives accusations of "homework" (It isn't) or answering a different question unrelated to the one asked (use an array of values, not a tuple! etc.). This one is wholly about the use of defaults with dictionaries in Swift.

– WishIHadThreeGuns
Mar 27 at 2:24





It's hugely problematic here, as the minimum requirement is clearly stated. I've given an extended example, but frequently doing so on Stack Overflow gives accusations of "homework" (It isn't) or answering a different question unrelated to the one asked (use an array of values, not a tuple! etc.). This one is wholly about the use of defaults with dictionaries in Swift.

– WishIHadThreeGuns
Mar 27 at 2:24













For what it's worth it's easier to understand what you're asking and what you're trying to achieve after you edited your question to include additional information.

– David Rönnqvist
Mar 27 at 3:00





For what it's worth it's easier to understand what you're asking and what you're trying to achieve after you edited your question to include additional information.

– David Rönnqvist
Mar 27 at 3:00












1 Answer
1






active

oldest

votes


















0














Is this what you looking for? Just get the current value with default value and store it in a variable first makes life easier.



var freq = [Int:(Int,Int,Int)]()

var nums = [1,2,2,3,3,3]
for num in nums.enumerated()

let currentTuple = freq[num.element, default: (0,num.offset,num.offset)]
freq[num.element] = (currentTuple.0 + 1, currentTuple.1,num.offset)

print(freq) //output: [1: (1, 0, 0), 2: (2, 1, 2), 3: (3, 3, 5)]





share|improve this answer
























    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%2f55368501%2fdefault-dictionary-value-in-swift%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Is this what you looking for? Just get the current value with default value and store it in a variable first makes life easier.



    var freq = [Int:(Int,Int,Int)]()

    var nums = [1,2,2,3,3,3]
    for num in nums.enumerated()

    let currentTuple = freq[num.element, default: (0,num.offset,num.offset)]
    freq[num.element] = (currentTuple.0 + 1, currentTuple.1,num.offset)

    print(freq) //output: [1: (1, 0, 0), 2: (2, 1, 2), 3: (3, 3, 5)]





    share|improve this answer





























      0














      Is this what you looking for? Just get the current value with default value and store it in a variable first makes life easier.



      var freq = [Int:(Int,Int,Int)]()

      var nums = [1,2,2,3,3,3]
      for num in nums.enumerated()

      let currentTuple = freq[num.element, default: (0,num.offset,num.offset)]
      freq[num.element] = (currentTuple.0 + 1, currentTuple.1,num.offset)

      print(freq) //output: [1: (1, 0, 0), 2: (2, 1, 2), 3: (3, 3, 5)]





      share|improve this answer



























        0












        0








        0







        Is this what you looking for? Just get the current value with default value and store it in a variable first makes life easier.



        var freq = [Int:(Int,Int,Int)]()

        var nums = [1,2,2,3,3,3]
        for num in nums.enumerated()

        let currentTuple = freq[num.element, default: (0,num.offset,num.offset)]
        freq[num.element] = (currentTuple.0 + 1, currentTuple.1,num.offset)

        print(freq) //output: [1: (1, 0, 0), 2: (2, 1, 2), 3: (3, 3, 5)]





        share|improve this answer













        Is this what you looking for? Just get the current value with default value and store it in a variable first makes life easier.



        var freq = [Int:(Int,Int,Int)]()

        var nums = [1,2,2,3,3,3]
        for num in nums.enumerated()

        let currentTuple = freq[num.element, default: (0,num.offset,num.offset)]
        freq[num.element] = (currentTuple.0 + 1, currentTuple.1,num.offset)

        print(freq) //output: [1: (1, 0, 0), 2: (2, 1, 2), 3: (3, 3, 5)]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 2:35









        Ricky MoRicky Mo

        2,4721 gold badge2 silver badges13 bronze badges




        2,4721 gold badge2 silver badges13 bronze badges



















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            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%2f55368501%2fdefault-dictionary-value-in-swift%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

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현