how to find index of tuple? - ios

i have myrecord class ..i need to find the index of object from tuple array
var transactionsGroupedByDate = [(String,Array<MyCushyRecords>)]()
where my object is myrecord object.

var transactionsGroupedByDate = [(String,Array<MyCushyRecords>)]()
i just changed Array of MyCushyRecords class to [Int] to finding index of tuples cause i don't know what exact your model does something like below
var transactionsGroupedByDate = [(String,[Int])]()
Now you can get index value like below,
var index:Int? = nil
for i in 0..<transactionsGroupedByDate.count {
let result = zip(transactionsGroupedByDate[i].1, YOUR_VALUE).enumerated().filter() {
$1.0 == $1.1
}.map{$0.0}
// result gives matches index value from both Int arrays.
if result.count == YOUR_VALUE.count {
index = i
break
}
}
//here you can check index of specific value in tuples
if index != nil {
print(index ?? "failed")
}else {
print("No matched values")
}

Related

iterate through two custom arrays and set values if variables are equal Swift

I have an easy question that is also hard at the same time. I have two separate structs (this can also work for classes):
struct FBTweet {
var tweetId: Int? //set
var tweetText: String? //set
}
and
struct Status {
var statusId: Int? //set
var statusText: String? //no value
}
I have an array of both structs var fbTweetArray: [FBTweet] = [] and var statusArray: [Status] = []
I have set every variable in to a certain value in each index in fbTweetArray but I only set the .statusId variable in each index for statusArray. For every statusArray.statusId value in statusArray, there is only one fbTweetArray.tweetId that has the same exact Int value. I am trying to make is so that if these two variables are the same then I should set set
statusArray.statusText to whatever fbTweetarray.tweetText is. So for example only fbTweetArray[1].tweetid = 2346 and statusArray[4].statusId = 2346 have 2346 as their value. There for if fbTweetArray[1].tweetText = "hello friend" then statusArray[4].statusText needs to be set to "hello friend".
So far I have
func testWhat () {
var fbTweetArray: [FBTweet] = []
var statusArray: [Status] = []
for fbTweet in fbTweetArray {
for var status in statusArray {
if (status.statusId == fbTweet.tweetId ) {
status.statusText = fbTweet.tweetText
}
}
}
}
how do I set the for var status in the for loop back into the statusArray since it is now a var and is different than one of the indexes in var statusArray: [Status] = []
Basically, you need only one for/forEach loop to achieve what you want:
var fbTweetArray: [FBTweet] = [
FBTweet(tweetId: 1, tweetText: "1"),
FBTweet(tweetId: 2, tweetText: "2"),
FBTweet(tweetId: 3, tweetText: "3")
]
var statusArray: [Status] = [
Status(statusId: 2, statusText: nil),
Status(statusId: 1, statusText: nil),
Status(statusId: 3, statusText: nil)
]
fbTweetArray.forEach { tweet in
if let index = statusArray.index(where: { $0.statusId == tweet.tweetId }) {
statusArray[index].statusText = tweet.tweetText
}
}
print(statusArray.map { $0.statusText }) // [Optional("2"), Optional("1"), Optional("3")]
Note, that your ids in both structures can be nil. To handle this situation (if both id is nil - objects are not equal) you can write custom == func:
struct Status {
var statusId: Int? //set
var statusText: String? //no value
static func == (lhs: Status, rhs: FBTweet) -> Bool {
guard let lhsId = lhs.statusId, let rhsId = rhs.tweetId else { return false }
return lhsId == rhsId
}
}
...
// rewrite .index(where: ) in if condition
if let index = statusArray.index(where: { $0 == tweet }) { ... }
Also, there is some pro-tip. If you adopt your structs to Hashable protocol, you will be able to place FBTweets and Statuses into Set structure. The benefits of that:
If you instead store those objects in a set, you can theoretically
find any one of them in constant time (O(1)) — that is, a lookup on a
set with 10 elements takes the same amount of time as a lookup on a
set with 10,000.
You can find more in-depth info about it in a new great article by NSHipster.
Your question is interesting only if both the arrays are not ordered.
To find the element from fbTweet array, you can sort it and employ binary search.
Then enumerate status array and find the fbTweet object with the same identifier and modify the status object. It needs to be saved again in the array as structs get copied on write.
extension Array where Element == FBTweet {
func binarySearchFBTweetWith(_ id:Int) -> FBTweet? {
var range = 0..<self.count
while range.startIndex < range.endIndex {
let midIndex = range.startIndex + (range.endIndex - range.startIndex) / 2
guard let tweetId = self[midIndex].tweetId else {
continue
}
if tweetId == id {
return self[midIndex]
} else if tweetId < id {
range = midIndex+1..<range.endIndex
} else {
range = range.startIndex..<midIndex
}
}
return nil
}
}
fbTweetArray.sort{($0.tweetId ?? 0) < ($1.tweetId ?? 0)}
for (index, status) in statusArray.enumerated() {
guard let statusId = status.statusId else {continue}
guard let fbTweet = fbTweetArray.binarySearchFBTweetWith(statusId) else {continue}
var status = status
status.statusText = fbTweet.tweetText
statusArray[index] = status
}
An alternative would be use dictionaries instead of arrays, for better performance and easier implementation (in this case). You can easily get the array of keys and values later If you need.
In this case, the Id would be the Key of the dictionary, and the text the value.

Find match in array for given string?

Ok so lets say I have an custom object for vocabulary words, alternate way of being written, and their meaning.
class VocabEntry {
var kanji:String?
var kana:String?
var meaning:String?
}
Then I have an array comprised of these objects. Here's one for example.
let newVocabWord = VocabEntry()
newVocabWord.kanji = "下さい”
newVocabWord.kana = "ください”
newVocabWord.meaning = "please"
Now I have a string of text:
let testString = "すみません、十階のボタンを押して下さい"
How can I compare that string to my array of custom objects (that contain strings) and reference the matches?
I tried.
if vocabArray.contains( { $0.kanji == testString }) {
print("yes")
}
But that trying to match the entire string. If I change testString to "下さい" it works, but that's not what I'm looking for. What I want is for it to say "Here I found 下さい in xx object. Here's the index number."
You can use indexOf() with a predicate to find the index of a
matching entry, and containsString() to search for substrings.
Since the kanji property is optional, you have to check that via
optional binding:
if let index = vocabArray.indexOf({ entry in
if let kanji = entry.kanji {
// check if `testString` contains `kanji`:
return testString.containsString(kanji)
} else {
// `entry.kanji` is `nil`: no match
return false
}
}) {
print("Found at index:", index)
} else {
print("Not found")
}
This can be written more concise as
if let index = vocabArray.indexOf({
$0.kanji.flatMap { testString.containsString($0) } ?? false
}) {
print("Found at index:", index)
} else {
print("Not found")
}
To get the indices of all matching entries, the following would work:
let matchingIndices = vocabArray.enumerate().filter { (idx, entry) in
// filter matching entries
entry.kanji.flatMap { testString.containsString($0) } ?? false
}.map {
// reduce to index
(idx, entry) in idx
}
print("Found at indices:", matchingIndices)

If filter returns nil, how do I not return empty result?

I'm doing the following filtering that returns a recipe list, filtered by given category name value.
filteredRecipe = filteredRecipe.filter({
if let category = $0.valueForKey("category") as? NSManagedObject {
if let name = category.valueForKey("name") as? String {
return name.rangeOfString(cap) != nil
} else {
return false
}
} else {
return false
}
})
This filter works in association with searchBar textfield so I will possibly have random value in the textfield which will make filteredRecipe to hold unreliable data.
I need to make sure when the filter can't find any recipe from filteredRecipe with given category name value, I leave filteredRecipe untouched.
Currently, when there is no match, it makes filteredRecipe empty array []. I'm not sure what part causes this.
You need to assign the filtering result to a temporary variable and check that it isn't empty.
let filtered = filteredRecipe.filter({
if let category = $0.valueForKey("category") as? NSManagedObject {
if let name = category.valueForKey("name") as? String {
return name.rangeOfString(cap) != nil
}
return false
})
if !filtered.isEmpty {
filteredRecipe = filtered
}
Another approach is to extend Collection with a selfOrNilIfEmpty property, and use the nil coalescing operator:
extension Collection {
var selfOrNilIfEmpty: Self? {
return isEmpty ? nil : self
}
}
and later, on your code:
let filteredRecipe = filteredRecipe.filter { ... }.selfOrNilIfEmpty ?? filteredRecipe

Access GrandChild of Multidimensional Array in Swift

I have a multi-dimensional array. It gets like [["1","2","3"]], how can I access "1"?
To get more in detail...
lazy var data : [[NSURL]] = {
var array = [[NSURL]]()
if array.count == 0 {
var index = 0
var section = 0
for image in images {
if array.count <= section {
array.append([NSURL]())
}
array[section].append(image)
index += 1
}
}
return array
}()
With print(data), I can access:
[["firstUrl", "secondUrl", "thirdUrl"]]
How can I access "first"?
You can do it like this: array[0][0]
For example enumerate like this:
var dict = [["1","2","3"]]
for (index, item) in dict.enumerate() {
//hear you enumerate []
for (index, item) in item.enumerate() {
//hear you enumerate ["1","2","3"]
print("Found \(item) at position \(index)")
}
}

Count optional properties in Swift

Is it possible to count all properties who are not nil?
For example:
class test {
var val1:Int?
var val2:Int?
var val3:Int?
var val4:Int?
var val5:Int?
}
var test = test()
test.val1 = 1
test.val2 = 2
How to find out that 2 properties are set? I could check for each one with (!= nil) - but is there an easier (and better) way?
You can do this manually, with a convenience method:
func numberOfNonNil() -> Int {
let vals = [val1, val2, val3, val4, val5]
return flatMap { $0 }.count
}
flatMap(_:) takes a closure that takes a single element of the array and returns an optional value (Element -> T?), and returns the result of applying that closure to each element of the array, with nil values ignored.
The only way to make this simpler would be to store your values as an array of optional Ints in the first place:
class Test {
var vals: [Int?]
}
You can then still access each individual value using the array subscript notation (let val2 = vals[1]). You could then just use the second line in the convenience method above (filter then count) to get the number of non-nil values:
let nonNilCount = vals.flatMap { $0 }.count
If your values are of different types, this approach will still work if you cast the array to a type that encompasses all the different types:
class Test {
var val1: Int?
var val2: Double
var val3: String
var val4: MyRandomClass?
func numberOfNonNil() -> Int {
let vals = [val1, val2, val3, val4, val5] as [Any?]
return flatMap { $0 }.count
}
}
This works because all the values can be expressed as the type Any?.
I don't think there's a way to do this, but you can implement your own function like this:
class test {
var val1:Int?
var val2:Int?
var val3:Int?
var val4:Int?
var val5:Int?
func setVarsCount() -> Int {
var setVariablesCount = 0
if val1 != nil {
setVariablesCount++
}
if val2 != nil {
setVariablesCount++
}
if val3 != nil {
setVariablesCount++
}
if val4 != nil {
setVariablesCount++
}
if val5 != nil {
setVariablesCount++
}
return setVariablesCount
}
}
#Stuarts answer is good, but you have to know the properties of the class, and if you add another property to the class, you also have to modify your method. To avoid this problem, you can use reflection, like
Swift 1.2:
func numberOfOptionalProperties() -> Int {
let mirror = reflect(self)
var numberOfOptionalProperties = 0
for index in 0..<mirror.count {
if mirror[index].1.disposition == .Optional {
++numberOfOptionalProperties
}
}
return numberOfOptionalProperties
}
Swift 2.0:
func numberOfOptionalProperties() -> Int {
return Mirror(reflecting: self).children.reduce(0) {
Mirror(reflecting: $1.value).displayStyle == .Optional ? $0 + 1 : $0
}
}

Resources