2

I am trying to go back and forth with different simple objective-c projects and port them to swift.

In objective-c, I have the following loop.

NSCountedSet *firstSet = [[NSCountedSet alloc] init];
NSCountedSet *secondSet = [[NSCountedSet alloc] init];
for(int i = 0; i < firstString.length; i++) {
    [firstSet addObject:@([firstString characterAtIndex:i])];
    [secondSet addObject:@([theSecondString characterAtIndex:i])];
}

I am attempting to port it to swift, but cannot figure out the addObject methodology

  let firstSet = NSCountedSet()
  let secondSet = NSCountedSet()
  let lengthOfFirstString = firstString.utf16Count  
  for var i = 0; i < lengthOfFirstString; i++ {

  }

Help on this would be appreciated

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
qweqweqwe
  • 343
  • 5
  • 8
  • 18
  • Don't remove your question contents. It is not only for you personally but also for future readers of this thread. – Martin R Feb 18 '15 at 19:46

2 Answers2

4

Not pretty, but here goes:

let firstSet = NSCountedSet()
let secondSet = NSCountedSet()
let lengthOfFirstString = firstString.utf16Count
for var i = 0; i < lengthOfFirstString; i++ {
    firstString[advance(firstString.startIndex, i)]
    theSecondString[advance(theSecondString.startIndex, i)]
}

Swift will hopefully include better functionality for substrings in future versions.

Mellson
  • 2,918
  • 1
  • 21
  • 23
-1

Please try the following code:

let firstSet = NSCountedSet()
let secondSet = NSCountedSet()
let lengthOfFirstString = firstString.utf16Count
for var i = 0; i < lengthOfFirstString; i++ {
    firstSet.addObject(@[firstString[i]])
    secondSet.addObject(@[secondString[i]])
}
kenorb
  • 155,785
  • 88
  • 678
  • 743
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215