Share via


NSDictionary 构造函数

定义

重载

NSDictionary()

不带参数初始化此类的新实例的默认构造函数。

NSDictionary(NSCoder)

从 unarchiver 对象中存储的数据初始化 对象的构造函数。

NSDictionary(NSDictionary)

从现有 NSDictionary 创建新的 NSDictionary。

NSDictionary(NSObjectFlag)

在派生类上调用 的构造函数,以跳过初始化并仅分配 对象。

NSDictionary(NSUrl)

根据位于指定 URL 处的 PropertyList 的内容创建新字典。

NSDictionary(IntPtr)

创建非托管对象的托管表示形式时使用的构造函数;由运行时调用。

NSDictionary(String)

根据位于指定文件中的 PropertyList 的内容创建新字典。

NSDictionary(NSDictionary, Boolean)
NSDictionary(NSUrl, NSError)
NSDictionary(NSObject, NSObject, NSObject[])

从 NSObject 键和 NSObject 值的列表创建 NSDictionary。

NSDictionary(Object, Object, Object[])

从键和值列表创建 NSDictionary。

NSDictionary()

不带参数初始化此类的新实例的默认构造函数。

[Foundation.Export("init")]
[ObjCRuntime.DesignatedInitializer]
public NSDictionary ();
属性

适用于

NSDictionary(NSCoder)

从 unarchiver 对象中存储的数据初始化 对象的构造函数。

[Foundation.Export("initWithCoder:")]
[ObjCRuntime.DesignatedInitializer]
public NSDictionary (Foundation.NSCoder coder);
new Foundation.NSDictionary : Foundation.NSCoder -> Foundation.NSDictionary

参数

coder
NSCoder

unarchiver 对象。

属性

注解

提供此构造函数是为了允许从非架构 (初始化类,例如,在 NIB 反序列化) 期间。 这是协议的一 NSCoding 部分。

如果开发人员想要创建此对象的子类并继续支持从存档反序列化,则应实现具有相同签名的构造函数:采用类型的 NSCoder 单个参数,并使用 [Export (“initWithCoder:”] 属性声明修饰它。

也可以使用配套方法 EncodeTo 对此对象的状态进行序列化。

适用于

NSDictionary(NSDictionary)

从现有 NSDictionary 创建新的 NSDictionary。

[Foundation.Export("initWithDictionary:")]
public NSDictionary (Foundation.NSDictionary other);
new Foundation.NSDictionary : Foundation.NSDictionary -> Foundation.NSDictionary

参数

other
NSDictionary

源字典。

属性

适用于

NSDictionary(NSObjectFlag)

在派生类上调用 的构造函数,以跳过初始化并仅分配 对象。

protected NSDictionary (Foundation.NSObjectFlag t);
new Foundation.NSDictionary : Foundation.NSObjectFlag -> Foundation.NSDictionary

参数

t
NSObjectFlag

未使用的 sentinel 值,传递 NSObjectFlag.Empty。

注解

当派生类在托管代码中完全构造对象并且仅希望运行时分配和初始化 NSObject 时,应调用此构造函数。 这是实现 Objective-C 使用的两步初始化过程所必需的,第一步是执行对象分配,第二步是初始化对象。 当开发人员调用采用 NSObjectFlag.Empty 的构造函数时,他们利用直接路径(一直一直指向 NSObject)来仅分配对象的内存并将 Objective-C 和 C# 对象绑定在一起。 对象的实际初始化由开发人员决定。

绑定生成器通常使用此构造函数来分配对象,但会阻止实际初始化。 分配完成后,构造函数必须初始化 对象。 对于绑定生成器生成的构造函数,这意味着它会手动调用其中一个“init”方法来初始化对象。

如果对象使用 NSObjectFlag.Empty 路径链接起来,则开发人员负责完全初始化对象。

通常,如果开发人员的构造函数调用 NSObjectFlag.Empty 基实现,则它应调用 Objective-C init 方法。 如果不是这种情况,开发人员应改为链接到其类中的适当构造函数。

参数值将被忽略,仅确保执行的唯一代码是构造阶段是基本的 NSObject 分配和运行时类型注册。 通常,链接如下所示:

//
// The NSObjectFlag merely allocates the object and registers the
// C# class with the Objective-C runtime if necessary, but no actual
// initXxx method is invoked, that is done later in the constructor
//
// This is taken from Xamarin.iOS's source code:
//
[Export ("initWithFrame:")]
public UIView (System.Drawing.RectangleF frame) : base (NSObjectFlag.Empty)
{
// Invoke the init method now.
	var initWithFrame = new Selector ("initWithFrame:").Handle;
	if (IsDirectBinding)
		Handle = ObjCRuntime.Messaging.IntPtr_objc_msgSend_CGRect (this.Handle, initWithFrame, frame);
	else
		Handle = ObjCRuntime.Messaging.IntPtr_objc_msgSendSuper_CGRect (this.SuperHandle, initWithFrame, frame);
}

适用于

NSDictionary(NSUrl)

根据位于指定 URL 处的 PropertyList 的内容创建新字典。

[Foundation.Export("initWithContentsOfURL:")]
public NSDictionary (Foundation.NSUrl url);
new Foundation.NSDictionary : Foundation.NSUrl -> Foundation.NSDictionary

参数

url
NSUrl

包含 PropertyList 格式的 NSDictionary 的资源的 URL。

属性

适用于

NSDictionary(IntPtr)

创建非托管对象的托管表示形式时使用的构造函数;由运行时调用。

protected internal NSDictionary (IntPtr handle);
new Foundation.NSDictionary : nativeint -> Foundation.NSDictionary

参数

handle
IntPtr

nativeint

指向非托管对象的指针 (句柄) 。

注解

此构造函数由运行时基础结构 (GetNSObject(IntPtr)) 调用,以便为指向非托管 Objective-C 对象的指针创建新的托管表示形式。 开发人员不应直接调用此方法,而是应调用 GetNSObject 方法,因为它将阻止托管对象的两个实例指向同一本机对象。

适用于

NSDictionary(String)

根据位于指定文件中的 PropertyList 的内容创建新字典。

[Foundation.Export("initWithContentsOfFile:")]
public NSDictionary (string fileName);
new Foundation.NSDictionary : string -> Foundation.NSDictionary

参数

fileName
String

包含属性列表格式字典的文件。

属性

适用于

NSDictionary(NSDictionary, Boolean)

[Foundation.Export("initWithDictionary:copyItems:")]
public NSDictionary (Foundation.NSDictionary other, bool copyItems);
new Foundation.NSDictionary : Foundation.NSDictionary * bool -> Foundation.NSDictionary

参数

other
NSDictionary
copyItems
Boolean
属性

适用于

NSDictionary(NSUrl, NSError)

[Foundation.Export("initWithContentsOfURL:error:")]
[ObjCRuntime.Introduced(ObjCRuntime.PlatformName.WatchOS, 4, 0, ObjCRuntime.PlatformArchitecture.All, null)]
[ObjCRuntime.Introduced(ObjCRuntime.PlatformName.TvOS, 11, 0, ObjCRuntime.PlatformArchitecture.All, null)]
[ObjCRuntime.Introduced(ObjCRuntime.PlatformName.MacOSX, 10, 13, ObjCRuntime.PlatformArchitecture.All, null)]
[ObjCRuntime.Introduced(ObjCRuntime.PlatformName.iOS, 11, 0, ObjCRuntime.PlatformArchitecture.All, null)]
public NSDictionary (Foundation.NSUrl url, out Foundation.NSError error);
new Foundation.NSDictionary : Foundation.NSUrl *  -> Foundation.NSDictionary

参数

url
NSUrl
error
NSError
属性

适用于

NSDictionary(NSObject, NSObject, NSObject[])

从 NSObject 键和 NSObject 值的列表创建 NSDictionary。

public NSDictionary (Foundation.NSObject first, Foundation.NSObject second, params Foundation.NSObject[] args);
new Foundation.NSDictionary : Foundation.NSObject * Foundation.NSObject * Foundation.NSObject[] -> Foundation.NSDictionary

参数

first
NSObject

第一个键。

second
NSObject

第一个值。

args
NSObject[]

键和值的剩余 pais。

注解

键和值列表用于创建字典。 传递给此函数的参数数必须为偶数。

var key1 = new NSString ("key1");
var value1 = new NSNumber ((byte) 1);
var key2 = new NSString ("key2");
var value2 = new NSNumber ((byte) 2);

var dict2 = new NSDictionary (key1, value1, key2, value2);

适用于

NSDictionary(Object, Object, Object[])

从键和值列表创建 NSDictionary。

public NSDictionary (object first, object second, params object[] args);
new Foundation.NSDictionary : obj * obj * obj[] -> Foundation.NSDictionary

参数

first
Object

第一个键。

second
Object

第一个值。

args
Object[]

键和值的剩余 pais。

注解

通过调用 FromObject(Object)将每个 C# 对象装箱为 NSObject。

键和值列表用于创建字典。 传递给此函数的参数数必须为偶数。

//
// Using C# objects, strings and ints, produces
// a dictionary with 2 NSString keys, "key1" and "key2"
// and two NSNumbers with the values 1 and 2
//
var dict = new NSDictionary ("key1", 1, "key2", 2);

适用于