-1

I'm a fresher in iOS

I'm unable to compare in an if statement. It looks like:

for i in 0..  <array.count

{ 

if(array[i] == array[i+1])

{

let removedVal = array.remove(i+1)

}

}

The error shows on the if condition:

Binary operator '==' cannot be applied to two 'Any' operands

I googled it, but I am unable to understand what should I do in my case.

======================================================================= Atlast able to find a solution. And it worked for me

if ( ((tempArrayForTeamName[i]) as AnyObject).isEqual(tempArrayForTeamName[i+1] as AnyObject) )

need to compare array index position as Any object And use .isEqual replace of ==

  • 1
    How is defined `array`? – Larme Oct 23 '19 at 13:09
  • var array = NSMutableArray() – Sanchyan Chakraborty Oct 23 '19 at 13:10
  • you can not modify same array you are using in for-loop iterations. – dahiya_boy Oct 23 '19 at 13:10
  • 2
    And what's inside the array? Use Swift Array in Swift3+ avoid using NSStuff when possible – Larme Oct 23 '19 at 13:11
  • In ( if ) condition, an error occurs. What should I do ? In my array, there are multiple same values. I need to delete those extra same string values. What should I do ? – Sanchyan Chakraborty Oct 23 '19 at 13:14
  • If you don't care about order, you can use `Set`. Else, look for "Remove duplicate Array Swift", you should get some response. If it's an array of Strings, then declare `let array: [String] = []` then. – Larme Oct 23 '19 at 13:16
  • possible duplicate of https://stackoverflow.com/questions/25738817/removing-duplicate-elements-from-an-array-in-swift/34712330?r=SearchResults&s=1|33.7045#34712330 – Leo Dabus Oct 23 '19 at 13:44
  • Check this: https://stackoverflow.com/questions/34778950/how-to-compare-any-value-types – Florian Tousch Oct 23 '19 at 13:55
  • Even if you are going to use Swift arrays your code will crash for several reasons. – vadian Oct 23 '19 at 14:06
  • I don't think others have mentioned this clearly enough. DON'T. I REPEAT. DON'T use `NSMutableArray`. Ditch that tutorial. It's way too old. arrays are defined as simple as: `var arr = [1,2,3]` or `var arr: [Int] = []` or `var arr: [Int] = [1,2,3]` or `var arr: Array = [1,2,3]`. These are all the same. They ALL use the `Array` type. But yours is different. Using `NSMutableArray ` is the Objetive-C way of defining arrays. Not that you shouldn't _know_ about it, but it's just not something that would offer value to a beginner... – mfaani Oct 23 '19 at 16:50

3 Answers3

0

In Swift : Array is a Generic Structure, NSMutableArray is an Objective-C class[will work in Swift].

A NSMutableArray created is of type Any; an array that can contain heterogenous object(could be String, Int or Bool).

An Array is arbitrarily specilized to contain Any (using as [Any])

eg:

 var array:Array = ["ABC", 123, true] as [Any]
    var nsMutableArray : NSMutableArray = ["ABC", 123, true]  

Generic Parameterization:

Even if there is an option to give generic parameterization(Datatype) to your NSMutableArray in Objective C[remember NSMutableArray in an Objective C class],this generic parameterization in unfortunately ignored/not allowed in Swift. How to specify the datatype: In Swift one cannot specify the datatype of a NSMutableArray.It would give a compilation error: Cannot specialize non-generic type NSMutableArray. But one can always specify the datatype of Array(Swift structure) as say: Array<String>. eg: var array:Array<String> = ["Tom", "Jerry", "Spike"]

eemrah
  • 1,603
  • 3
  • 19
  • 37
0

You have to Filter your Array

var newarray = [Int]()

let dictionary = ["A":0,"B":1,"C":1,"D":1,"E":1,"F":1,"G":1,"H":1,"J":0]

let newDictionary = dictionary.reduce([:]) { result, element -> [String: Int] in
guard element.value != 1 else {

    return result
}
var newResult = result
newResult[element.key] = element.value
newarray.append(newResult[element.key]!)

return newResult

}

0

Your code has another problem, consider your array has 3 items then i=2, and you are trying to access index 3 (i+1). And program will crash.

Crash point (array[i] == array[i+1])

Please declare specific types array for example

let myArray:[String] = ["a", "b", "c", "d"]