Guides and Sample Code

Developer

Using Swift with Cocoa and Objective-C (Swift 4.1)

iBooks
On This Page

Working with Cocoa Frameworks
Cocoaフレームワークを扱う

As part of its interoperability with Objective-C, Swift offers convenient and efficient ways of working with Cocoa frameworks.
それのObjective-Cとの相互運用性の一部として、スウィフトはCocoaフレームワークを扱う便利で効率的な方法を提供します。

Swift automatically converts some Objective-C types to Swift types, and some Swift types to Objective-C types. Types that can be converted between Objective-C and Swift are referred to as bridged types. For example, in Swift code, you can pass a String value to an Objective-C method declared to take an NSString parameter. In addition, many of the Cocoa frameworks, including Foundation, AppKit, and UIKit refine their APIs to be more natural in Swift. For example, the NSCoder method decodeObjectOfClass(_:forKey:) uses Swift generics to provide a stronger type signature.
スウィフトは、いくつかのObjective-C型をスウィフト型に、そしていくつかのスウィフト型をObjective-C型に自動的に変換します。Objective-Cとスウィフトの間で変換されることが可能な型は、ブリッジ(橋渡しされる)型と呼ばれます。例えば、スウィフトコードにおいて、あなたはあるString値を、NSStringパラメータをとるように宣言されたObjective-Cメソッドに渡すことができます。加えて、Foundation、AppKit、そしてUIKitを含む、多くのCocoaフレームワークは、それらのAPIをSwiftにおいてより自然なものに改良されます。例えば、NSCoderのメソッドdecodeObjectOfClass(_:forKey:)は、スウィフト総称体を使うことでより強力な型シグネチャを提供します。

Foundation
ファウンデーション

The Foundation framework provides a base layer of functionality for apps and frameworks, including data storage, text processing, dates and times, sorting and filtering, persistence, and networking.
Foundationフレームワークは、ある基盤階層をアプリとフレームワークのための機能性に関して提供していて、データストレージ、テキスト処理、日付と時刻、ソートとフィルタ、永続、そしてネートワーク処理を含みます。

Bridged Types
ブリッジ型

The Swift Foundation overlay provides the following bridged value types for the following Objective-C reference types:
スウィフト Foundationオーバーレイは、以下のブリッジ値型を以下のObjective-C参照型に対して提供します:

Objective-C Reference Type
Objective-C参照型

Swift Value Type
スウィフト値型

NSAffineTransform

AffineTransform

NSArray

Array

NSCalendar

Calendar

NSCharacterSet

CharacterSet

NSData

Data

NSDateComponents

DateComponents

NSDateInterval

DateInterval

NSDate

Date

NSDecimalNumber

Decimal

NSDictionary

Dictionary

NSIndexPath

IndexPath

NSIndexSet

IndexSet

NSLocale

Locale

NSMeasurement

Measurement

NSNotification

Notification

NSNumber

Swift numeric types (Int, Float, and so on.)
スウィフト数値型(IntFloat、など)。

NSPersonNameComponents

PersonNameComponents

NSSet

Set

NSString

String

NSTimeZone

TimeZone

NSURL

URL

NSURLComponents

URLComponents

NSURLQueryItem

URLQueryItem

NSURLRequest

URLRequest

NSUUID

UUID

These value types have the same functionality as their corresponding reference types. Class clusters that include immutable and mutable subclasses are bridged to a single value type. Swift code uses var and let to control mutability, so it doesn’t need both classes. The corresponding reference types can be accessed with their original NS class name prefix.
これらの値型は、それらの対応する参照型と同じ機能性を持ちます。不変および可変サブクラスを含むクラス群は、単一値型へブリッジされます。スウィフトコードは、varletを使って可変性を制御します、それでそれは両方のクラスを必要としません。対応する参照型は、それら本来のNSクラス名接頭辞でアクセスされることができます。

Anywhere you can use a bridged Objective-C reference type, you can use the Swift value type instead. This lets you take advantage of the functionality available on the reference type’s implementation in a way that is natural in Swift code. For this reason, you should almost never need to use a bridged reference type directly in your own code. In fact, when Swift code imports Objective-C APIs, the importer replaces Objective-C reference types with their corresponding value types. Likewise, when Objective-C code imports Swift APIs, the importer also replaces Swift value types with their corresponding Objective-C reference types.
あなたがブリッジObjective-C参照型を使うことができるどこにでも、あなたはスウィフト値型をその代わりに使うことができます。これは、あなたに参照型の実装上で利用可能な機能性を、スウィフトコードにおける自然な方法で利用させます。この理由のために、あなたはブリッジ参照型を直接にあなた自身のコードにおいて使う必要はほとんどないはずです。実際、スウィフトコードがObjective-C APIをインポートするとき、インポーターはObjective-C参照型をそれらの対応する値型と置き換えます。同様に、Objective-Cコードがスウィフト APIをインポートするとき、インポーターはまたスウィフト値型をそれらの対応するObjective-C参照型と置き換えます。

One of the primary advantages of value types over reference types is that they make it easier to reason about your code. For more information about value types, see Classes and Structures in The Swift Programming Language (Swift 4.1), and WWDC 2015 session 414 Building Better Apps with Value Types in Swift.
参照型を超える値型の主要な利点の1つは、それらがあなたのコードについて考えるのをより簡単にすることです。値型についてのさらなる情報として、クラスと構造体スウィフトプログラミング言語 (Swift 4.1)で、そしてWWDC 2015 セッション 414 より良いアプリをスウィフトの値型で作り上げるを見てください。

If you do need to use a bridged Foundation object, you can cast between bridged types using the as type casting operator.
あなたがブリッジFoundationオブジェクトを使うことをどうしても必要とするならば、あなたはブリッジ型の間でキャストすることがas型キャスト演算子を使って可能です。

Renamed Types
改名された型

The Swift Foundation overlay renames classes and protocols, as well as related enumerations and constants.
スウィフト Foundationオーバーレイは、クラスおよびプロトコルを改名します、それだけでなく関連した列挙と定数も。

Imported Foundation classes and protocols drop their NS prefix, with the following exceptions:
インポートされたFoundationのクラスおよびプロトコルは、それらのNS接頭辞を外します、以下の例外を除いては:

  • Classes specific to Objective-C or inherently tied to the Objective-C runtime, such as NSObject, NSAutoreleasePool, NSException, and NSProxy
    Objective-C特有の、またはObjective-Cランタイムに本質的に結びつけられ切り離せないクラス、例えばNSObjectNSAutoreleasePoolNSException、そしてNSProxyなど。

  • Platform-specific classes, such as NSBackgroundActivity, NSUserNotification, and NSXPCConnection
    プラットホーム特有のクラス、例えばNSBackgroundActivityNSUserNotification、そしてNSXPCConnectionなど。

  • Classes that have a value type equivalent, as described in Bridged Types, such as NSString, NSDictionary, and NSURL
    ブリッジ型で記述される、値型の相当物を持つクラス、例えばNSStringNSDictionary、そしてNSURLなど。

  • Classes that do not have a value type equivalent but are planned to have one in the near future, such as NSAttributedString, NSRegularExpression, and NSPredicate
    値型の相当物を持たないが近い将来それを持つ計画をされるクラス、例えばNSAttributedStringNSRegularExpression、そしてNSPredicate

Foundation classes often declare enumeration or constant types. When importing these types, Swift moves them to be nested types of their related types. For example, the NSJSONReadingOptions option set is imported as JSONSerialization.ReadingOptions.
Foundationクラスは、しばしば列挙や定数型を宣言します。それらの型をインポートするとき、スウィフトはそれらを移動して、それらの関連型の入れ子にされた型にします。例えば、NSJSONReadingOptionsオプションセットはJSONSerialization.ReadingOptionsとしてインポートされます。

Strings
文字列

Swift bridges between the String type and the NSString class. You can create an NSString object by casting a String value using the as operator. You can also create an NSString object using a string literal by explicitly providing a type annotation.
スフィフトは、String型とNSStringクラスの間をブリッジします。あなたは、NSStringオブジェクトの作成が、String値をas演算子を使ってキャストすることで可能です。あなたはまた、明示的に型注釈を提供することによって文字列リテラルを使ってNSStringオブジェクトを作成できます。

  1. import Foundation
  2. let string: String = "abc"
  3. let bridgedString: NSString = string as NSString
  4. let stringLiteral: NSString = "123"
  5. if let integerValue = Int(stringLiteral as String) {
  6. print("\(stringLiteral) is the integer \(integerValue)")
  7. }
  8. // Prints "123 is the integer 123"

Numbers

Swift bridges between the NSNumber class and Swift numeric types, including Int, Double, and Bool.
スウィフトはNSNumberクラスと、IntDouble、およびBoolを含むスウィフト数値型の間をブリッジします。

You can create an NSNumber object by casting a Swift number value using the as operator. Because NSNumber can contain a variety of different types, you must use the as? operator when casting to a Swift number type. For example, casting an NSNumber value representing the number 500 to the Int8 Swift type will fail and return nil, because the largest value that an Int8 value can represent is 127.
あなたは、NSNumberオブジェクトを作成することが、スウィフト数値をas演算子を使ってキャストすることによって可能です。NSNumberが様々な異なる型を含むことができることから、あなたはスウィフト数値型をキャストする時にas?演算子を使わなければなりません。例えば、数500を表しているNSNumber値をInt8のSwift型へキャストすることは失敗してnilを返します、なぜならInt8値が表すことができる最大値は127だからです。

You can also create an NSNumber object using a floating-point, integer, or Boolean literal by explicitly providing a type annotation.
あなたはまた、NSNumberオブジェクトを作成することが、浮動小数点、整数、またはブールのリテラルを使って明示的に型注釈を提供することによって可能です。

  1. import Foundation
  2. let number = 42
  3. let bridgedNumber: NSNumber = number as NSNumber
  4. let integerLiteral: NSNumber = 5
  5. let floatLiteral: NSNumber = 3.14159
  6. let booleanLiteral: NSNumber = true

Arrays
配列

Swift bridges between the Array type and the NSArray class. When you bridge from an NSArray object with a parameterized type to a Swift array, the element type of the resulting array is bridged as well. If an NSArray object does not specify a parameterized type, it is bridged to a Swift array of type [Any].
スウィフトは、Array型とNSArrayクラスの間での橋渡しをします。あなたがあるパラメーター化された型を使うNSArrayオブジェクトからスウィフト配列へとブリッジするとき、結果として生じる配列の要素型も同様にブリッジされます。NSArrayオブジェクトがパラメータ化された型を指定しないならば、それは型[Any]のスウィフト配列に橋渡しされます。

For example, consider the following Objective-C declarations:
例として、以下のObjective-C宣言を考えてください:

  1. @property NSArray *objects;
  2. @property NSArray<NSDate *> *dates;
  3. - (NSArray<NSDate *> *)datesBeforeDate:(NSDate *)date;
  4. - (void)addDatesParsedFromTimestamps:(NSArray<NSString *> *)timestamps;

Here’s how Swift imports them:
ここにどのようにスウィフトがそれらをインポートするかがあります:

  1. var objects: [Any]
  2. var dates: [Date]
  3. func datesBeforeDate(date: Date) -> [Date]
  4. func addDatesParsedFromTimestamps(timestamps: [String])

You can also create an NSArray object directly from a Swift array literal, following the same bridging rules outlined above. When you explicitly type a constant or variable as an NSArray object and assign it an array literal, Swift creates an NSArray object instead of a Swift array.
あなたはまた、スウィフト配列リテラルから直接NSArrayオブジェクトをつくることが、上で概説されるのと同じ橋渡し規則にしたがって可能です。あなたが明示的に定数または変数をNSArrayオブジェクトとして型指定して、それに配列リテラルを代入するとき、スウィフトはスウィフト配列でなくてNSArrayオブジェクトをつくります。

  1. let schoolSupplies: NSArray = ["Pencil", "Eraser", "Notebook"]
  2. // schoolSupplies is an NSArray object containing three values (schoolSuppliesは3つの値を含んでいるNSArrayオブジェクトです)

Sets
集合

In addition to arrays, Swift bridges between the Set type and the NSSet class. When you bridge from an NSSet object with a parameterized type to a Swift set, the resulting set is of type Set<ObjectType>. If an NSSet object does not specify a parameterized type, it is bridged to a Swift set of type Set<AnyHashable>.
配列に加えて、スウィフトはSet型とNSSetクラスの間を橋渡しします。あなたがあるパラメーター化された型を使うNSSetオブジェクトからスウィフト集合へ橋渡しするとき、結果として生じる集合は型Set<ObjectType>です。NSSetオブジェクトがパラメーター化された型を指定しないならば、それは型Set<AnyHashable>のスウィフト集合に橋渡しされます。

For example, consider the following Objective-C declarations:
例として、以下のObjective-C宣言を考えてください:

  1. @property NSSet *objects;
  2. @property NSSet<NSString *> *words;
  3. - (NSSet<NSString *> *)wordsMatchingPredicate:(NSPredicate *)predicate;
  4. - (void)removeWords:(NSSet<NSString *> *)words;

Here’s how Swift imports them:
ここにどのようにスウィフトがそれらをインポートするかがあります:

  1. var objects: Set<AnyHashable>
  2. var words: Set<String>
  3. func wordsMatchingPredicate(predicate: NSPredicate) -> Set<String>
  4. func removeWords(words: Set<String>)

You can also create an NSSet object directly from a Swift array literal, following the same bridging rules outlined above. When you explicitly type a constant or variable as an NSSet object and assign it an array literal, Swift creates an NSSet object instead of a Swift set.
あなたはまた、NSSetオブジェクトを直接にスウィフト配列リテラルから作成することが、上で概説されるのと同じ橋渡し規則にしたがって可能です。あなたが明示的に定数または変数をNSSetオブジェクトとして型指定して、それに配列リテラルを代入するとき、スウィフトはスウィフト集合でなくてNSSetオブジェクトをつくります。

  1. let amenities: NSSet = ["Sauna", "Steam Room", "Jacuzzi"]
  2. // amenities is an NSSet object containing three values (amenitiesは3つの値を含んでいるNSSetオブジェクトです)

Dictionaries
辞書

Swift also bridges between the Dictionary type and the NSDictionary class. When you bridge from an NSDictionary object with parameterized types to a Swift dictionary, the resulting dictionary is of type [Key: Value]. If an NSDictionary object does not specify parameterized types, it is bridged to a Swift dictionary of type [AnyHashable: Any].
スウィフトはまた、Dictionary型からNSDictionaryクラスへ橋渡しします。あなたがパラメーター化された型たちを使うあるNSDictionaryオブジェクトからスウィフト辞書へ橋渡しするとき、結果として得られる辞書は型[Key: Value]です。NSDictionaryオブジェクトがパラメーター化された型を指定しないならば、それは型[AnyHashable: Any]のスウィフト辞書に橋渡しされます。

For example, consider the following Objective-C declarations:
例として、以下のObjective-C宣言を考えてください:

  1. @property NSDictionary *keyedObjects;
  2. @property NSDictionary<NSURL *, NSData *> *cachedData;
  3. - (NSDictionary<NSURL *, NSNumber *> *)fileSizesForURLsWithSuffix:(NSString *)suffix;
  4. - (void)setCacheExpirations:(NSDictionary<NSURL *, NSDate *> *)expirations;

Here’s how Swift imports them:
ここにどのようにスウィフトがそれらをインポートするかがあります:

  1. var keyedObjects: [AnyHashable: Any]
  2. var cachedData: [URL: Data]
  3. func fileSizesForURLsWithSuffix(suffix: String) -> [URL: NSNumber]
  4. func setCacheExpirations(expirations: [URL: NSDate])

You can also create an NSDictionary object directly from a Swift dictionary literal, following the same bridging rules outlined above. When you explicitly type a constant or variable as an NSDictionary object and assign it a dictionary literal, Swift creates an NSDictionary object instead of a Swift dictionary.
あなたはまた、NSDictionaryオブジェクトを直接にスウィフト辞書リテラルから作成することが、上で概説されるのと同じ橋渡し規則にしたがって可能です。あなたが明示的に定数または変数をNSDictionaryオブジェクトとして型指定して、それに辞書リテラルを代入するとき、スウィフトはスウィフト辞書でなくてNSDictionaryオブジェクトをつくります。

  1. let medalRankings: NSDictionary = ["Gold": "1st Place", "Silver": "2nd Place", "Bronze": "3rd Place"]
  2. // medalRankings is an NSDictionary object containing three key-value pairs (medalRankingsは、NSDictionaryオブジェクトでキー値ペアを含んでいます)

Core Foundation
コアファウンデーション

Core Foundation types are imported as Swift classes. Wherever memory management annotations have been provided, Swift automatically manages the memory of Core Foundation objects, including Core Foundation objects that you instantiate yourself. In Swift, you can use each pair of toll-free bridged Foundation and Core Foundation types interchangeably. You can also bridge some toll-free bridged Core Foundation types to Swift standard library types if you cast to a bridging Foundation type first.
コアファウンデーション型は、スウィフトクラスとしてインポートされます。メモリ管理注釈が提供されているどこででも、あなた自身でインスタンス化するコアファウンデーション・オブジェクトを含めて、スウィフトは自動的にコアファウンデーション・オブジェクトのメモリを管理します。スウィフトにおいて、あなたは、無料の橋渡しされたファウンデーションとコアファウンデーション型のそれぞれの対を入れ替えて使うことができます。あなたはまた、いくつかの無料の橋渡しされたコアファウンデーション型をスウィフト標準のライブラリ型に橋渡しをすることが、橋渡ししているファウンデーション型にあなたが最初にキャストするならば、可能です。

Remapped Types
リマップされた型

When Swift imports Core Foundation types, the compiler remaps the names of these types. The compiler removes Ref from the end of each type name because all Swift classes are reference types, therefore the suffix is redundant.
スウィフトがコアファウンデーション型をインポートするとき、コンパイラはこれらの型の名前をリマップします。コンパイラは各型名の終わりからRefを削除します、なぜなら全てのスウィフトクラスが参照型であり、したがってこの接尾辞は冗長です。

The Core Foundation CFTypeRef type completely remaps to the AnyObject type. Wherever you would use CFTypeRef, you should now use AnyObject in your code.
コアファウンデーションのCFTypeRef型は、AnyObject型に完全にリマップされます。あなたがCFTypeRefを使うどこであろうとも、あなたは現在あなたのコードにおいてAnyObjectを使わなければなりません。

Memory Managed Objects
メモリ管理オブジェクト

Core Foundation objects returned from annotated APIs are automatically memory managed in Swift—you do not need to invoke the CFRetain, CFRelease, or CFAutorelease functions yourself.
注釈をつけられたAPIから返されるコアファウンデーション・オブジェクトは、自動的にスウィフトにおいてメモリ管理されます ― あなたは、あなた自身でCFRetainCFRelease、またはCFAutorelease関数を呼び出す必要はありません。

If you return Core Foundation objects from your own C functions and Objective-C methods, you can annotate them with either the CF_RETURNS_RETAINED or CF_RETURNS_NOT_RETAINED macro to automatically insert memory management calls. You can also use the CF_IMPLICIT_BRIDGING_ENABLED and CF_IMPLICIT_BRIDGING_DISABLED macros to enclose C function declarations that follow Core Foundation ownership policy naming policy in order to infer memory management from naming.
あなたがコアファウンデーション・オブジェクトをあなた独自のC関数およびObjective-Cメソッドから返すならば、あなたはそれらにCF_RETURNS_RETAINEDまたはCF_RETURNS_NOT_RETAINEDマクロのどちらかで注釈をつけて、自動的にメモリ管理呼び出しを差し込むことができます。あなたはまた、CF_IMPLICIT_BRIDGING_ENABLEDCF_IMPLICIT_BRIDGING_DISABLEDマクロを使って、C関数宣言たち、それらは名前の付け方からメモリ管理を推測するためのコアファウンデーション所有者方針の命名方針に従うもの、を取り囲むことができます。

If you use only annotated APIs that do not indirectly return Core Foundation objects, you can skip the rest of this section. Otherwise, continue on to learn about working with unmanaged Core Foundation objects.
あなたがコアファウンデーション・オブジェクトを間接的に返さない注釈をつけられたAPIだけを使うならば、あなたはこの節の残りをスキップすることができます。一方で、管理されないコアファウンデーション・オブジェクトを扱うことについて学ぶためには続けてください。

Unmanaged Objects
管理されないオブジェクト

When Swift imports APIs that have not been annotated, the compiler cannot automatically memory manage the returned Core Foundation objects. Swift wraps these returned Core Foundation objects in an Unmanaged<Instance> structure. All indirectly returned Core Foundation objects are unmanaged as well. For example, here’s an unannotated C function:
注釈をつけられなかったAPIをスウィフトがインポートするとき、コンパイラは返されたコアファウンデーション・オブジェクトを自動的にメモリ管理できません。スウィフトは、これらの返されたコアファウンデーション・オブジェクトをUnmanaged<T>構造体に包みます。全ての間接的に返されたコアファウンデーション・オブジェクトは、同様に管理されません。例えば、ここに注釈をつけられなかったC関数があります:

  1. CFStringRef StringByAddingTwoStrings(CFStringRef s1, CFStringRef s2)

And here’s how Swift imports it:
そしてここにスウィフトがそれをインポートする方法があります:

  1. func StringByAddingTwoStrings(_: CFString!, _: CFString!) -> Unmanaged<CFString>! {
  2. // ...
  3. }

When you receive an unmanaged object from an unannotated API, you should immediately convert it to a memory managed object before you work with it. That way, Swift can handle memory management for you. The Unmanaged<Instance> structure provides two methods to convert an unmanaged object to a memory managed object—takeUnretainedValue() and takeRetainedValue(). Both of these methods return the original, unwrapped type of the object. You choose which method to use based on whether the API you are invoking returns an unretained or retained object.
あなたが注釈をつけられなかったAPIから管理されないオブジェクトを受け取るとき、あなたがそれを扱う前に、あなたは直ちにそれをメモリ管理されたオブジェクトに変換しなければなりません。そのように、スウィフトはあなたの代わりにメモリ管理をうまく取り扱うことができます。Unmanaged<T>構造体は、管理されないオブジェクトをメモリ管理されたオブジェクトに変換するために、2つのメソッドを提供します ― takeUnretainedValue()takeRetainedValue()。これらのメソッドの両方とも、最初の、包装を取られた型のオブジェクトを返します。あなたは、あなたが呼んでいるAPIが保有されない、または保有されるオブジェクトを返すかに基づいて、どちらかのメソッドを選びます。

For example, suppose the C function above does not retain the CFString object before returning it. To start using the object, you use the takeUnretainedValue() function.
例えば、上のC関数がCFStringオブジェクトを、それを返す前に保持しないと思ってください。そのオブジェクトを使い始めるために、あなたはtakeUnretainedValue()関数を使用します。

  1. let memoryManagedResult = StringByAddingTwoStrings(str1, str2).takeUnretainedValue()
  2. // memoryManagedResult is a memory managed CFString (memoryManagedResultは、メモリ管理されるCFStringです)

You can also invoke the retain(), release(), and autorelease() methods on unmanaged objects, but this approach is not recommended.
あなたはまた、管理されないオブジェクトの上でretain()release()、そしてautorelease()メソッドを呼び出すことができます、しかしこの取り組みは推薦されません。

For more information, see Ownership Policy in Memory Management Programming Guide for Core Foundation.
更なる情報のために、所有者方針コアファウンデーションのためのメモリ管理プログラミングガイドで見てください。

Unified Logging
統合ログ

The unified logging system provides an API for capturing messaging across all levels of the system, and is a replacement for the NSLog function in the Foundation framework. Unified logging is available in iOS 10.0 and later, macOS 10.12 and later, tvOS 10.0 and later, and watchOS 3.0 and later.
統合ログシステムは、システムのすべての水準にまたがってメッセージを捕らえるためにAPIを提供します、そしてFoundationフレームワークにおけるNSLog関数に対する置き換えです。統合ログは、iOS 10.0以降、macOS 10.12以降、tvOS 10.0以降、そしてwatchOS 3.0以降で利用可能です。

In Swift, you can interact with the unified logging system using the top level os_log(_:dso:log:type:_:) function, found in the log submodule of the os module.
スウィフトでは、あなたは統合ログシステムとトップレベルのos_log(_:dso:log:type:_:)関数、osモジュールのlogサブモジュールで見つかります、を使ってやり取りできます。

  1. import os.log
  2. os_log("This is a log message.")

You can format a log message using an NSString or printf format string along with one or more trailing arguments.
あなたはログメッセージをフォーマットすることが、NSStringまたはprintfフォーマット文字列を1つ以上の後に続く引数とともに使うことで行えます。

  1. let fileSize = 1234567890
  2. os_log("Finished downloading file. Size: %{iec-bytes}d", fileSize)

You can also specify a log level defined by the logging system, such as Info, Debug, or Error, in order to control how log messages are handled according to the importance of the logging event. For example, information that may be helpful, but isn’t essential for troubleshooting errors should be logged at the Info level.
あなたはまた、ログイベントの重要性にそってどのようにログメッセージが取り扱われるか制御するために、ログシステムによって定義されるログ水準、例えばInfo、Debug、またはErrorを指定することが可能です。例えば、助けとなるかもしれない、しかしエラーを解決するのに必須ではない情報は、Info水準でログされるべきです。

  1. os_log("This is additional info that may be helpful for troubleshooting.", type: .info)

To log a message to a specific subsystem, you can create a new OSLog object, specifying the subsystem and category, and pass it as a parameter to the os_log function.
ある特定のサブシステムに対するメッセージをログするには、あなたは新しいOSLogオブジェクトを作成して、そのサブシステムとカテゴリを指定します、そしてそれをパラメータとしてos_log関数に渡します。

  1. let customLog = OSLog("com.your_company.your_subsystem_name.plist", "your_category_name")
  2. os_log("This is info that may be helpful during development or debugging.", log: customLog, type: .debug)

For more information about the unified logging system, see Logging.
統合ログシステムについてのさらなる情報として、Loggingを見てください。

Cocoa Structures
Cocoa構造体

When bridging from Swift code back into Objective-C code, built-in structures from Cocoa and Foundation are bridged as NSValue instances. As a result, you can use an Objective-C structure from Swift in Cocoa APIs that accept only instances of reference types. This is true even though the instance’s defining type is bridged to Swift as a structure type.
スウィフトコードから逆にObjective-Cコードへとブリッジする場合、CocoaおよびFoundation由来の組み込みの構造体はNSValueインスタンスとしてブリッジされます。結果として、あなたは参照型のインスタンスのみを受け取るCocoa APIにおいてスウィフト由来のObjective-C構造体を使うことができます。これは、たとえそのインスタンスのもつ定義型がスウィフトへ構造体型としてブリッジされるとしても当てはまります。

The following structures are bridged to NSValue:
以下の構造体はNSValueへブリッジされます:

  • CATransform3D

  • CLLocationCoordinate2D

  • CGAffineTransform

  • CGPoint

  • CGRect

  • CGSize

  • CGVector

  • CMTimeMapping

  • CMTimeRange

  • CMTime

  • MKCoordinateSpan

  • NSRange

  • SCNMatrix4

  • SCNVector3

  • SCNVector4

  • UIEdgeInsets

  • UIOffset