5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Objective-CからSwiftへの書き換え作業、型、ポインタ、配列Array、辞書Dictionary

Last updated at Posted at 2018-05-08

よく使うものだけをピックアップしました。
C言語の単純型とSwiftの型の相関関係

C言語の単純型 Swiftの型
Bool Bool
short int16
int int32
long Int
long long Int64
float Float
double Double

Objective-Cのポインタ型とSwiftの型

種類 C言語の型 Swiftの型
ポインタ const T * UnsafePointer
T * UnsafeMutablePointer
const void * UnsafeRawPointer
void * UnsafeMutableRawPointer
クラスCのインスタンス C * const * UnsafePointer
C * __strong UnsafeMutablePointer
C ** AutoreleasingUnsafeMutablePointer
関数 T (*func)(U) @convention(c)(U) -> T
T (*func)(void) @convention(c)() -> T
ブロック T (^block)(U) @convention(block)(U) -> T

Array から NSArray への変換方法


let intList:[Int] = [1,2,3,4]
let cArray = intList as NSArray

NSArray から Array への変換方法


let ns1: NSArray = [100, "sample", 200]
let ns2: NSArray = [10, 20, 30]

if let ar1 = ns1 as? [Any] {
    print(type(of: ar1))
}

if let ar2 = ns2 as? [Int] {
    print(type(of: ar2))
}

配列arrayから文字列だけを取り出したい場合の便利な方法


let strList = array.filter{ $0 is String }

Swift のDictionaryはそのままNSDictionaryに変換可能。
NSDictionaryが
複数の型のキーを含まなければならない場合は AnyHashable型のキー
値が複数の型の場合は Any型の値
をもつ辞書を作れば変換可能


let d:[AnyHashable: Any] = [AnyHashable(0): "Zero",
                            AnyHashable("II"): 2]
let nsDic = d as NSDictionary

詳細は「詳解Swift」を参考にして書きました

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?