-1
내 응용 프로그램에는 Objective C로 작성된 일부 기본 레거시 코드가 있지만 겹치는 메서드는 Swift로 작성되었습니다.Swift와 Objective C 배열 사이의 변환
이제 스위프트 코드는 스위프트 배열 [Int]
을 사용하지만 목표 C 방법은 NSArray
및 NSMutableArray
이 필요합니다.
다른 배열 유형을 변환/변환하는 방법은 무엇입니까?
스위프트 코드 :
/* The remaining orders are finally stored as [Int] */
static var remainingOrders:[Int]! = [Int]()
/* Handles the orders loaded event */
@objc static func ordersLoaded(notification:Notification) {
let userInfo:Dictionary = notification.userInfo as! Dictionary<String, Any>
let orders:NSArray = userInfo["orders"] as! NSArray // orders is of type NSArray
let remainingOrders:NSMutableArray = orderUtils.filterRemainingOrders(fromOrders: orders.mutableCopy() as! NSMutableArray); // but it needs to be passed as NSMutableArray
self.remainingOrders = remainingOrders as! [Int]; // the NSMutableArray needs to be stored as [Int]
downloadService.download([Any](self.remainingOrders)); // then remaining orders need to be passed as NSArray
}
목표 C 메소드 서명은 (수정할 수 없습니다) : (self.remainingOrders를 캐스팅
/* The remaining orders are finally stored as [Int] */
static var remainingOrders:[Int]! = [Int]()
/* Handles the orders loaded event */
@objc static func ordersLoaded(notification:Notification) {
let userInfo:Dictionary = notification.userInfo as! Dictionary<String, Any>
let orders:NSArray = userInfo["orders"] as! NSArray // orders is of type NSArray
let remainingOrders:NSMutableArray = orderUtils.filterRemainingOrders(fromOrders: orders.mutableCopy() as! NSMutableArray); // but it needs to be passed as NSMutableArray
self.remainingOrders = remainingOrders as NSArray as! [Int]; // the NSMutableArray needs to be stored as [Int]
downloadService.download(self.remainingOrders); // then remaining orders need to be passed as NSArray
}
시도 : 여기
–'self.remainingOrders = remainingOrders를 NSArray로! [Int];'해당 라인에서 작동합니다. –