0

I am very new to Swift. I have a mutable dictionary where I need to do a copy on it. I have the code in Objective-C and when I try to convert that to Swift it says Value of type [AnyHashable] has no member 'copy'. Can someone help me convert below ObjC code to Swift 4.

This is the original Objective-C code:

NSMutableArray *months = [[NSMutableArray alloc] init];

NSDictionary *monthDictionary = [[NSDictionary alloc] initWithObjects:@[[months copy]] forKeys:@[@"values"]];

Below is what I am trying when converting to Swift 4:

var months = [AnyHashable]()

var monthDictionary = [AnyHashable: Any](objects: [months.copy()], forKeys: ["values"])

Cannot use 'copy' here.

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
AK1188
  • 17
  • 6
  • see this , it helps you https://stackoverflow.com/questions/27812433/how-do-i-make-a-exact-duplicate-copy-of-an-array – Anbu.Karthik May 17 '18 at 08:10
  • What types of data is the months? In Swift, both arrays and dictionaries are value types, so depending on what a "month" is, you might not need to explicitly copy them. – David Rönnqvist May 17 '18 at 08:28
  • Which type is `months` supposed to contain? Don't try to translate ObjC to Swift literally? In most cases there are better native APIs. – vadian May 17 '18 at 09:12
  • months is NsmutableArray – AK1188 May 17 '18 at 09:25
  • I can see that `months` is `NSMutableArray`. My question was what **static** type does the array **contain**? Swift is based on a strong type system. – vadian May 18 '18 at 08:54
  • It contains Strings.12 months from January to December. – AK1188 May 18 '18 at 12:33

0 Answers0