2

My app in Xcode with swift language programming :

I have a struct like:

struct PageFilter {
    var key: Int?
    var title: NSString?
}

And then I have the values in: filters are coming from API and i am saving them to extractedFilter

if let filters = filters {
    for filter in filters {

        var extractedFilter = PageFilter()
        extractedFilter.key = filter["key"].integerValue
        extractedFilter.title = filter["title"].stringValue 

    }
}

I have an array of page filter like :

lazy var availableFilters = Array<PageFilter>()

I want to fill the availableFilters with ExtractedFilter.

******* *i fixed the issue by a loop like this code :

 var strFilter : String = ""

            for var i = 0; i < self.newFilterList.availableGuildFilters.count; i++ {
                let guildFilter = self.newFilterList.availableGuildFilters[i]
                if guildFilter.selected {
                    strFilter += "\(guildFilter.key),"
                }
            }

thanks to all*

4
  • if let filters = filters { ... your code here filters it is not optional } thats how you unwrap an optional Commented Apr 22, 2015 at 11:17
  • i want to do like this availableFilters = Extended Filter but i can not , cause extendedFilter is not array ! Commented Apr 22, 2015 at 11:19
  • You are not even showing where and how you declared extendedFilter. The better the question the easier for people to understand you issue. You should try improving your question Commented Apr 22, 2015 at 11:25
  • i mean ExtractedFilter * Commented Apr 22, 2015 at 11:32

3 Answers 3

1

The following Swift 1.2 playground code would do it - I have put in a function to simulate the call to the API

//: Playground - noun: a place where people can play

import Cocoa

struct PageFilter {
    var key: Int?
    var title: NSString?
}

// this would be replaced by whatever way you get your filters from the API
func getFiltersFromApi() -> [PageFilter]? {
    // return nil // uncomment this line to demo the API returning nothing
    return [PageFilter(key: 1, title: "one"),
        PageFilter(key: 2, title: "two"),
        PageFilter(key: 3, title: "three"),
        PageFilter(key: nil, title: nil)
    ]
}

let filters: [PageFilter]? = getFiltersFromApi() // API call, this could return nil
let extractedFilters: [PageFilter]

if let filters = filters {
    extractedFilters = filters.map { filter in
        PageFilter(key: filter.key, title: filter.title)
    }
} else {
    extractedFilters = []
}

for filter in extractedFilters {
    println("key: \(filter.key), title: \(filter.title)")
}

Alternatively you could have your lazy var like this

var availableFilters: [PageFilter] = {
    let filters: [PageFilter]? = getFiltersFromApi() // API call, this could return nil

    if let filters = filters {
        return filters.map { filter in
            PageFilter(key: filter.key, title: filter.title)
        }
    } else {
        return []
    }
}()

The code is similar to Leonardo's answer, the main difference being the use of the map function instead of for ... in ...

Sign up to request clarification or add additional context in comments.

Comments

0

Try like this:

struct PageFilter {
    var key = Int()
    var title = String()
}


var filters:[PageFilter]? = []
filters = [PageFilter(key: 1, title: "one"), PageFilter(key: 2, title: "two"), PageFilter(key: 3, title: "three")]

var extractedFilter = Array<PageFilter>()
if let filters = filters {
    for filter in filters {
        extractedFilter.append(PageFilter(key: filter.key, title: filter.title))
    }
}

println(extractedFilter[1].key)   // "2"
println(extractedFilter[1].title) // "two"

5 Comments

You could use map on the filters array to create the extractedFilter array e.g. let extractedFilter = filters.map { filter in PageFilter(key: filter.key, title: filter.title) } this would cut down on the for in section :)
stackoverflow.com/questions/29782982/… why don't you show me how to use filter and map?
I was going to post my answer but thought that as it was similar to yours (except for the map part) that instead I would comment and suggest an alternative to part of it. Apparently suggesting that is off limits...?
That's ok I just think it would be easier for the OP to understand it like this.
Ok I'll post it as an answer instead
0

I fixed the issue by a loop like this:

var strFilter : String = ""
for var i = 0; i < self.newFilterList.availableGuildFilters.count; i++ {
    let guildFilter = self.newFilterList.availableGuildFilters[i]
    if guildFilter.selected {
        strFilter += "\(guildFilter.key),"
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.