KVC技术指南 ---ios官方

所需积分/C币:21 2014-11-10 15:47:15 492KB PDF
4
收藏 收藏
举报

KVC技术指南 ---ios官方
Scalar and Structure Support 31 Representing Non-Object Values 31 Handling nil values 31 Wrapping and Unwrapping Scalar Types 32 Wrapping and Unwrapping Structs 33 Collection Operators 35 Data Used In Examples 36 Simple Collection Operators 37 @avg 37 @count 37 @max 37 @min 38 @sum 38 Object Operators 38 @distinctUnion Ofobjects 39 QunionOfobjects 39 Array and Set Operators 39 @distinctIon OfArrays 40 @unionOfArrays 41 @distinctUnionOfSets 41 Accessor Search Implementation Details 43 Accessor Search Patterns for Simple Attributes 43 Default Search Pattern for setValue: forKey: 43 Default Search Pattern for value ForKey: 43 Accessor Search Pattern for Ordered Collections 44 Accessor Search Pattern for Uniquing Ordered Collections 45 Accessor Search Pattern for Unordered Collections 46 Describing Property Relationships 48 Class descriptions 48 Performance Considerations 49 Overriding Key-Value Coding Methods 49 Optimizing To-Many Relationships 49 Document Revision History 50 2012-07-17 Copyright O 2003, 2012 Apple InC. All Rights Reserved 3 Figures, Tables, and Listings What Is Key-Value Coding? 8 Listing 1 Implementation of data-source method without key-value coding 9 Listing 2 Implementation of data-source method with key-value coding 10 Key-Value Coding Accessor Methods 15 Listing 1 Accessor naming variations for a hidden property key 15 Listing 2 Alternate form accessor for a hidden property key 15 Listing 3 Accessor naming convention to support a hidden property key 16 Listing 4 Example -count<Key> implementation 18 Listing 5 Example -objectIn<KeysAtIndex: and -<keysAtIndexes: implementations 18 Listing 6 Example ->: range: implementation 19 Listing 7 Example -insertobject: in<Key>AtIndex: and -insert<Key>:atIndexes: accessors 20 Listing 8 Example -removeobjectFrom<Key>AtIndex: and -remove<KeysAtIndexes: accessors 20 Listing 9 Example -replaceObjectIn<KeysAtIndex: withobject: and rep lace<KeysAtIndexes: with<Keys: accessors 21 Listing 10 Example-countof<Key>, -enumeratorof<Key>, and -memberof<Key>: accessors 22 Listing 11 Example -add<Key>object: and -add<Key>: accessors 23 Listing 12 Example -remove<Key>Object: and -remove<Key>: accessors 23 Listing 13 Example -intersect<Key>: and -set<Key>: implementations 24 Key-Value Validation 25 Listing 1 Validation method declaration for the property name 25 Listing 2 Validation method for the name property 26 Listing 3 Validation method for a scalar property 27 Scalar and Structure Support 31 Table 1 Scalar types as wrapped in NSNumber objects 32 Table 2 Common struct types as wrapped using NSValue. 33 Listing 1 Example implementation of setNilValueForKey: 32 Collection Operators 35 Figure 1 Operator key path format 35 2012-07-17 Copyright o 2003, 2012 Apple Inc. All Rights Reserved 4 Table 1 Example data for the Transactions objects 36 Table 2 Hypothetical Transaction data in the mo reTransactions array 40 2012-07-17 Copyright O 2003, 2012 Apple InC. All Rights Reserved 5 Introduction This document describes the nsKey a lue Coding informal protocol, which defines a mechanism allowing applications to access the properties of an object indirectly by name (or key) rather than directly through invocation of an accessor method or as instance variables You should read this document to gain an understanding of how to use key-value coding in your applications Id how to make your classes key-value coding compliant for interacting with other technologies. Key-value coding is a fundamental technology when working with key-value observing, Cocoa bindings, Core Data, and making your application AppleScript-able. You are expected to be familiar with the basics of Cocoa developmen and the objective-C language Organization of This document Key-value Coding includes these articles: What Is Key-Value Coding? (page 8)provides an overview of key-value coding Terminology (page 11) defines the terms commonly used to refer to an object's properties Key-Value Coding Fundamentals(page 12)describes the basic principles required to use key-value coding Key-Value Coding Accessor Methods (page 15)describes the accessor methods your classes should implement Key-Value Validation (page 25)tells you how to implement property validation Ensuring KvC Compliance(page 29 )describes the capabilities a class must implement to be key-value coding compliant Scalar and Structure Support(page 31) describes the data types supported by key-value coding Collection Operators (page 35)lists the available collection operators and describes their use Accessor Search Implementation Details (page 43)explains how the appropriate accessor method or instance variable is determined Describing Property Relationships (page 48 )describes the use of meta-data to define the relationships between objects and their properties Performance Considerations (page 49 )describes the performance considerations when using key-value coding. 2012-07-17 Copyright O 2003, 2012 Apple InC. All Rights Reserved 6 Introduction See also See also There are other technologies, not covered in this document, that are related to key-value coding Key-Value Observing Programming Guide describes the features of the key-value observing protocol that allows objects to observe changes in other objects 2012-07-17 Copyright O 2003, 2012 Apple InC. All Rights Reserved What Is Key-Value Coding? Key-value coding is a mechanism for accessing an object's properties indirectly, using strings to identify properties, rather than through invocation of an accessor method or accessing them directly through instance variables. In essence, key-value coding defines the patterns and method signatures that your applications accessor methods implement Accessor methods, as the name suggests, provide access to your application s data model s property values There are two basic forms of accessor -get accessors and set accessors. Get accessors, also referred to as getters, return the values of a property. Set accessors, also referred to as setters, set the value of a property There are getter and setter variants for dealing with both object attributes and to-many relationships Implementing key-value coding compliant accessors in your application is an important design principle Accessors help to enforce proper data encapsulation and facilitate integration with other technologies such as key-value observing, Core Data, Cocoa bindings, and scriptability. Key-value coding methods can, in many cases, also be utilized to simplify your application s code The essential methods for key-value coding are declared in the NSKey value Coding Objective-C informal protocol and default implementations are provided by Nsobject Key-value coding supports properties with object values, as well as the scalar types and structs Non-object parameters and return types are detected and automatically wrapped, and unwrapped, as described in Scalar and Structure Support(page 31) Key-value Coding and Scripting The scripting support in Cocoa is designed so that an application can easily implement scripting by accessing its model objects-the objects that encapsulate the applications data. When the user executes an AppleScript command that targets your application, the goal is for that command to go directly to the applications model objects to get the work done Scripting in OS X relies heavily on key-value coding to provide automatic support for executing Apple Script commands In a scriptable application, a model object defines a set of keys that it supports. Each key represents a property of the model object. Some examples of scripting- related keys are words, font, documents, and color. The key-value coding aPI provides a generic and automatic way to query an object for the values of its keys and to set new values for those keys 2012-07-17 Copyright O 2003, 2012 Apple InC. All Rights Reserved 8 What Is Key-Value Coding? Using Key-Value Coding to Simplify Your Code As you design the objects of your application, you should define the set of keys for your model objects and implement the corresponding accessor methods. Then when you define the script suites for your application, you can specify the keys that each scriptable class supports. If you support key-value coding, you will get a great deal of scripting support "for free In Applescript, object hierarchies define the structure of the model objects in an application Most applescript commands specify one or more objects within your application by drilling down this object hierarchy from parent container to child element You can define the relationships between properties available through key-value coding in class descriptions. See Describing Property Relationships(page 48 )for more details Cocoa's scripting support takes advantage of key-value coding to get and set information in scriptable objects The method in the objective-C informal protocol NSScriptKey va lue Coding provides additional capabilities for working with key-value coding, including getting and setting key values by index in multi-value keys and coercing(or converting) a key-value to the appropriate data type Using Key-Value Coding to Simplify Your Code You can use key-value coding methods in your own code to generalize implementations. For example, in OS X NSTableview and NSoutlineview objects both associate an identifier string with each of their columns By making this identifier the same as the key for the property you wish to display, you can significantly simplify your code Listing 1 shows an implementation of an NSTableview delegate method without using key-value coding Listing 2 shows an implementation that takes advantage of key-value coding to return the appropriate value using the column identifier as the key Listing 1 Implementation of data-source method without key-value coding (id)tableview:(NSTableview *)tableview objectvalueFortab leColumn:(id)column row: (NSInteger)row t Childobject *child [childrenarray objectatIndex: row] if ([[column identifier] isEqualTostring: @"name ])t return [child name if ([[co lumn identifier] isequalTostring: age ])t return [child age]; if ([[column identifier] isequalTostring: @favoriteColor ])t 2012-07-17 Copyright O 2003, 2012 Apple InC. All Rights Reserved 9 What Is Key-Value Coding? Using Key-Value Coding to Simplify Your Code return [child favoritecolorl: / And so or Listing 2 Implementation of data-source method with key-value coding (id)tableview: (NStableview *)tableview objectValueForTableCo lumn: (id)column row: (NSInteger)row t Childobject *child [childrenarray objectatIndex: row] return [child value Forkey: [co lumn identifier] 2012-07-17 Copyright O 2003, 2012 Apple InC. All Rights Reserved 0

...展开详情
试读 52P KVC技术指南 ---ios官方
立即下载 身份认证VIP会员低至7折
一个资源只可评论一次,评论内容不能少于5个字
您会向同学/朋友/同事推荐我们的CSDN下载吗?
谢谢参与!您的真实评价是我们改进的动力~
关注 私信
上传资源赚钱or赚积分
最新推荐
KVC技术指南 ---ios官方 21积分/C币 立即下载
1/52
KVC技术指南 ---ios官方第1页
KVC技术指南 ---ios官方第2页
KVC技术指南 ---ios官方第3页
KVC技术指南 ---ios官方第4页
KVC技术指南 ---ios官方第5页
KVC技术指南 ---ios官方第6页
KVC技术指南 ---ios官方第7页
KVC技术指南 ---ios官方第8页
KVC技术指南 ---ios官方第9页
KVC技术指南 ---ios官方第10页
KVC技术指南 ---ios官方第11页

试读结束, 可继续读5页

21积分/C币 立即下载