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
|
Swift Value Type
|
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Swift numeric types ( |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.
これらの値型は、それらの対応する参照型と同じ機能性を持ちます。不変および可変サブクラスを含むクラス群は、単一値型へブリッジされます。スウィフトコードは、var
とlet
を使って可変性を制御します、それでそれは両方のクラスを必要としません。対応する参照型は、それら本来の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
, andNSProxy
Objective-C特有の、またはObjective-Cランタイムに本質的に結びつけられ切り離せないクラス、例えばNSObject
、NSAutoreleasePool
、NSException
、そしてNSProxy
など。Platform-specific classes, such as
NSBackgroundActivity
,NSUserNotification
, andNSXPCConnection
プラットホーム特有のクラス、例えばNSBackgroundActivity
、NSUserNotification
、そしてNSXPCConnection
など。Classes that have a value type equivalent, as described in Bridged Types, such as
NSString
,NSDictionary
, andNSURL
ブリッジ型で記述される、値型の相当物を持つクラス、例えばNSString
、NSDictionary
、そしてNSURL
など。Classes that do not have a value type equivalent but are planned to have one in the near future, such as
NSAttributedString
,NSRegularExpression
, andNSPredicate
値型の相当物を持たないが近い将来それを持つ計画をされるクラス、例えばNSAttributedString
、NSRegularExpression
、そして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
オブジェクトを作成できます。
import Foundation
let string: String = "abc"
let bridgedString: NSString = string as NSString
let stringLiteral: NSString = "123"
if let integerValue = Int(stringLiteral as String) {
print("\(stringLiteral) is the integer \(integerValue)")
}
// Prints "123 is the integer 123"
Numbers
数
Swift bridges between the NSNumber
class and Swift numeric types, including Int
, Double
, and Bool
.
スウィフトはNSNumber
クラスと、Int
、Double
、および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
オブジェクトを作成することが、浮動小数点、整数、またはブールのリテラルを使って明示的に型注釈を提供することによって可能です。
import Foundation
let number = 42
let bridgedNumber: NSNumber = number as NSNumber
let integerLiteral: NSNumber = 5
let floatLiteral: NSNumber = 3.14159
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宣言を考えてください:
@property NSArray *objects;
@property NSArray<NSDate *> *dates;
- (NSArray<NSDate *> *)datesBeforeDate:(NSDate *)date;
- (void)addDatesParsedFromTimestamps:(NSArray<NSString *> *)timestamps;
Here’s how Swift imports them:
ここにどのようにスウィフトがそれらをインポートするかがあります:
var objects: [Any]
var dates: [Date]
func datesBeforeDate(date: Date) -> [Date]
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
オブジェクトをつくります。
let schoolSupplies: NSArray = ["Pencil", "Eraser", "Notebook"]
// 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宣言を考えてください:
@property NSSet *objects;
@property NSSet<NSString *> *words;
- (NSSet<NSString *> *)wordsMatchingPredicate:(NSPredicate *)predicate;
- (void)removeWords:(NSSet<NSString *> *)words;
Here’s how Swift imports them:
ここにどのようにスウィフトがそれらをインポートするかがあります:
var objects: Set<AnyHashable>
var words: Set<String>
func wordsMatchingPredicate(predicate: NSPredicate) -> Set<String>
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
オブジェクトをつくります。
let amenities: NSSet = ["Sauna", "Steam Room", "Jacuzzi"]
// 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宣言を考えてください:
@property NSDictionary *keyedObjects;
@property NSDictionary<NSURL *, NSData *> *cachedData;
- (NSDictionary<NSURL *, NSNumber *> *)fileSizesForURLsWithSuffix:(NSString *)suffix;
- (void)setCacheExpirations:(NSDictionary<NSURL *, NSDate *> *)expirations;
Here’s how Swift imports them:
ここにどのようにスウィフトがそれらをインポートするかがあります:
var keyedObjects: [AnyHashable: Any]
var cachedData: [URL: Data]
func fileSizesForURLsWithSuffix(suffix: String) -> [URL: NSNumber]
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
オブジェクトをつくります。
let medalRankings: NSDictionary = ["Gold": "1st Place", "Silver": "2nd Place", "Bronze": "3rd Place"]
// 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から返されるコアファウンデーション・オブジェクトは、自動的にスウィフトにおいてメモリ管理されます ― あなたは、あなた自身でCFRetain
、CFRelease
、または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_ENABLED
とCF_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関数があります:
CFStringRef StringByAddingTwoStrings(CFStringRef s1, CFStringRef s2)
And here’s how Swift imports it:
そしてここにスウィフトがそれをインポートする方法があります:
func StringByAddingTwoStrings(_: CFString!, _: CFString!) -> Unmanaged<CFString>! {
// ...
}
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()
関数を使用します。
let memoryManagedResult = StringByAddingTwoStrings(str1, str2).takeUnretainedValue()
// 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
サブモジュールで見つかります、を使ってやり取りできます。
import os.log
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つ以上の後に続く引数とともに使うことで行えます。
let fileSize = 1234567890
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水準でログされるべきです。
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
関数に渡します。
let customLog = OSLog("com.your_company.your_subsystem_name.plist", "your_category_name")
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
Writing Swift Classes and Protocols with Objective-C Behavior
スウィフトのクラスとプロトコルをObjective-C挙動で書く
Copyright © 2018 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2018-03-29