Ray's playground

 

02 2011 档案

Keyboard Events(Chapter 19 of Cocoa Programming for Mac OS X)
摘要:1#import"BigLetterView.h"234@implementationBigLetterView56-(id)initWithFrame:(NSRect)frameRect7{8if(![superinitWithFrame:frameRect])9{10returnnil;11}1213NSLog(@"initializingview");14bgColor=[[NSColoryellowColor]retain];15string=@"";16returnself;17}1819-(void)dealloc20{2 阅读全文

posted @ 2011-02-28 17:03 Ray Z 阅读(412) 评论(0) 推荐(0)

Memory Management(Chapter 3 of Objective-C Phrasebook)
摘要:When you send an object an -autoreleasemessage, it is added to the currently activeNSAutoreleasePool instance. When thisinstance is destroyed, every object added to itis sent a -release message.The -autorelease message is a deferred-release message. You send it to an objectwhen you no longer need a. 阅读全文

posted @ 2011-02-26 21:52 Ray Z 阅读(222) 评论(0) 推荐(0)

Item 40: Use Dynamic for Parameters That Receive Anonymous Types(Effective C#)
摘要:You can leverage dynamic to create methods that are intended to be usedwith anonymous types. It’s a technique to be used sparingly, like strongspices. If you find yourself creating many methods using dynamic invocationthat are intended for use with anonymous types, that’s a strong indicationthat yo. 阅读全文

posted @ 2011-02-26 10:38 Ray Z 阅读(202) 评论(0) 推荐(0)

Item 39: Use Dynamic to Leverage the Runtime Type of Generic Type Parameters(Effective C#)
摘要:Cast<T>, like all generic methods, compiles with only limited knowledgeof its type parameters. That can lead to generic methods not working theway you’d expect. The root cause is almost always that the generic methodcould not be made aware of particular functionality in the type representingth 阅读全文

posted @ 2011-02-25 23:56 Ray Z 阅读(255) 评论(0) 推荐(0)

Item 38: Understand the Pros and Cons of Dynamic(Effective C#)
摘要:There are also performance costs with using dynamic and with buildingexpressions at runtime. Just like any dynamic type system, your programhas more work to do at runtime because the compiler did not performany of its usual type checking. The compiler must generate instructions toperform all those . 阅读全文

posted @ 2011-02-25 22:36 Ray Z 阅读(251) 评论(0) 推荐(0)

Item 37: Construct Parallel Algorithms with Exceptions in Mind(Effective C#)
摘要:Exceptions are complicated in any algorithm. Parallel tasks create morecomplications. The Parallel Task Library uses the AggregateException classto hold any and all exceptions thrown somewhere in the depths of yourparallel algorithms. Once any of the background threads throws an exception,any other. 阅读全文

posted @ 2011-02-25 21:16 Ray Z 阅读(309) 评论(0) 推荐(0)

An Objective-C Primer(Chapter 2 of Objective-C Phrasebook)
摘要:#import<Foundation/Foundation.h>int(^getCounter(void))(void){__blockintcounter;int(^block)(void)=^(void){returncounter++;};return_Block_copy(block);}intmain(void){int(^block)(void)=getCounter();block();block();NSCAssert(block()==2,@"Blockcountedincorrectly");int(^block2)(void)=getCou 阅读全文

posted @ 2011-02-25 18:33 Ray Z 阅读(167) 评论(0) 推荐(0)

Item 36: Understand How to Use PLINQ for I/O Bound Operations(Effective C#)
摘要:The Parallel Task Library provides a series of methods that enable workingwith I/O bound operations as well as CPU bound partitions of work.Using the Task class, you can support a variety of asynchronous patternsthat work with I/O bound operations, or those that are a mixture of I/Oand CPU bound op. 阅读全文

posted @ 2011-02-24 22:37 Ray Z 阅读(213) 评论(0) 推荐(0)

Images and Mouse Events(Chapter 18 of Cocoa Programming for Mac OS X)
摘要:1#import"StretchView.h"234@implementationStretchView56-(id)initWithFrame:(NSRect)rect7{8if(![superinitWithFrame:rect])9{10returnnil;11}1213srandom(time(NULL));1415path=[[NSBezierPathalloc]init];16[pathsetLineWidth:3.0];17NSPointp=[selfrandomPoint];18[pathmoveToPoint:p];19inti;20for(i=0;i&l 阅读全文

posted @ 2011-02-24 16:53 Ray Z 阅读(262) 评论(0) 推荐(0)

Custom Views(Chapter 17 of Cocoa Programming for Mac OS X)
摘要:#import"StretchView.h"@implementationStretchView-(id)initWithFrame:(NSRect)rect{if(![superinitWithFrame:rect]){returnnil;}srandom(time(NULL));path=[[NSBezierPathalloc]init];[pathsetLineWidth:3.0];NSPointp=[selfrandomPoint];[pathmoveToPoint:p];inti;for(i=0;i<15;i++){p=[selfrandomPoint];[ 阅读全文

posted @ 2011-02-24 11:17 Ray Z 阅读(212) 评论(0) 推荐(0)

Item 35: Learn How PLINQ Implements Parallel Algorithms(Effective C#)
摘要:Every parallel query begins with a partitioning step. PLINQ needs to partitionthe input elements and distribute those over the number of taskscreated to perform the query. Partitioning is one of the most importantaspects of PLINQ, so it is important to understand the differentapproaches, how PLINQ . 阅读全文

posted @ 2011-02-23 20:48 Ray Z 阅读(200) 评论(0) 推荐(0)

Item 34: Avoid Overloading Methods Defined in Base Classes(Effective C#)
摘要:1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Text;56namespaceEffectiveCSharpItem347{8publicclassB2{}9publicclassD2:B2{}1011publicclassB12{13publicvoidFoo(D2parm)14{15Console.WriteLine("InB.Foo");16}17publicvoidBar(B2parm)18{19Console.WriteLine("InB.Bar& 阅读全文

posted @ 2011-02-23 20:08 Ray Z 阅读(194) 评论(0) 推荐(0)

Localization(Chapter 16 of Cocoa Programming for Mac OS X)
摘要:Clearly, as you develop and localize many applications, you will develop a set of common translations. It would be handy to have an automated way to get the translated strings into a nib file. This is one of several uses for ibtool. The ibtoolcommand, which is run from the terminal, can list the cl. 阅读全文

posted @ 2011-02-23 17:41 Ray Z 阅读(168) 评论(0) 推荐(0)

Using Alert Panels(Chapter 15 of Cocoa Programming for Mac OS X)
摘要:The NSRunAlertPanel() function returns an intthat indicates which button the user clicked. There are global variables for these constants: NSAlertDefaultReturn, NSAlertAlternateReturn, and NSAlertOtherReturn. int NSRunAlertPanel(NSString *title, NSString *msg, NSString *defaultButton, NSString *alt. 阅读全文

posted @ 2011-02-23 11:44 Ray Z 阅读(230) 评论(0) 推荐(0)

Using Notifications(Chapter 14 of Cocoa Programming for Mac OS X)
摘要:PreferenceControllerCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->1#import"PreferenceController.h"234@implementationPreferenceController56NSString*constBNRTableBgColorKey=@"TableBackgroundColor";7NSString*constBNREmptyDocKey= 阅读全文

posted @ 2011-02-23 11:15 Ray Z 阅读(296) 评论(0) 推荐(0)

Item 33: Use the new Modifier Only to React to Base Class Updates(Effective C#)
摘要:codeCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Text;56namespaceEffectiveCSharpItem337{8classBase9{10publicvoidMagicMethod()11{12Console.WriteLine("magicmethodfr 阅读全文

posted @ 2011-02-22 21:43 Ray Z 阅读(271) 评论(0) 推荐(0)

STL Containers & Iterators part2(Chapter 4 of Thinking in C++ Vol 2)
摘要:The vector is intentionally made to look like a souped-up array, since it has array-styleindexing but also can expand dynamically. vector is so fundamentally useful that it wasintroduced in a very primitive way early in this book, and used quite regularly in previousexamples. This section will give. 阅读全文

posted @ 2011-02-22 17:22 Ray Z 阅读(278) 评论(0) 推荐(0)

User Defaults(Chapter 13 of Cocoa Programming for Mac OS X)
摘要:Every application comes with a set of defaults from the factory. When a user edits his or her defaults, only the differences between the user's wishes and the factory defaults are stored in the user's defaults database. Thus, every time the application starts up, you need to remind it of the 阅读全文

posted @ 2011-02-22 13:27 Ray Z 阅读(169) 评论(0) 推荐(0)

Item 32: Avoid ICloneable(Effective C#)
摘要:ICloneable does have its use, but it is the exception rather than rule. It’s significantthat the .NET Framework did not add an ICloneable<T> when itwas updated with generic support. You should never add support forICloneable to value types; use the assignment operation instead. Youshould 阅读全文

posted @ 2011-02-21 21:06 Ray Z 阅读(180) 评论(0) 推荐(0)

Item 31: Implement Ordering Relations with IComparable<T> and IComparer<T>(Effective C#)
摘要:IComparable and IComparer are the standard mechanisms for providingordering relations for your types. IComparable should be used for themost natural ordering. When you implement IComparable, you shouldoverload the comparison operators (<, >, <=, >=) consistently with ourIComparable order 阅读全文

posted @ 2011-02-21 20:55 Ray Z 阅读(200) 评论(0) 推荐(0)

Nib Files and NSWindowController(Chapter 12 of Cocoa Programming for Mac OS X)
摘要:PreferenceControllerCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->1#import"PreferenceController.h"234@implementationPreferenceController56-(id)init7{8if(![superinitWithWindowNibName:@"Preferences"])9{10returnnil;11}1213returnself;14}1516-(void)w 阅读全文

posted @ 2011-02-21 17:25 Ray Z 阅读(273) 评论(0) 推荐(0)

Item 30: Prefer Overrides to Event Handlers(Effective C#)
摘要:When you have one function that handles one event in a derived class, theoverride is the better approach. It is easier to maintain, more likely to becorrect over time, and more efficient. Reserve the event handlers for otheruses. Prefer overriding the base class implementation to attaching an eventh 阅读全文

posted @ 2011-02-19 13:06 Ray Z 阅读(217) 评论(0) 推荐(0)

Item 29: Support Generic Covariance and Contravariance(Effective C#)
摘要:It certainly can get complicated describing exactly how covariance andcontravariance work. Thankfully, now the language supports decoratinggeneric interfaces and delegates with in (contravariant) and out (covariant)modifiers. You should decorate any interfaces and delegates you definewith the in or 阅读全文

posted @ 2011-02-19 11:12 Ray Z 阅读(150) 评论(0) 推荐(0)

Item 28: Create Large-Grain Internet Service APIs(Effective C#)
摘要:Nothing special. 阅读全文

posted @ 2011-02-17 21:28 Ray Z 阅读(140) 评论(0) 推荐(0)

Item 27: Prefer Making Your Types Serializable(Effective C#)
摘要:The .NET Framework provides a simple, standard algorithm for serializingyour objects. If your type should be persisted, you should follow thestandard implementation. If you don’t support serialization in your types,other classes that use your type can’t support serialization, either. Mak 阅读全文

posted @ 2011-02-17 21:13 Ray Z 阅读(247) 评论(0) 推荐(0)

Item 26: Avoid Returning References to Internal Class Objects(Effective C#)
摘要:You built the interfaceto your class, and you want users to follow it. You don’t want users to accessor modify the internal state of your objects without your knowledge.You’ve got four different strategies for protecting your internal data structuresfrom unintended modifications: value t 阅读全文

posted @ 2011-02-16 20:56 Ray Z 阅读(213) 评论(0) 推荐(0)

Item 25: Implement the Event Pattern for Notifications(Effective C#)
摘要:Events provide a standard syntax for notifying listeners. The .NET EventPattern follows the event syntax to implement the Observer Pattern. Anynumber of clients can attach handlers to the events and process them.Those clients need not be known at compile time. Events don’t need subscribersfor 阅读全文

posted @ 2011-02-16 20:40 Ray Z 阅读(233) 评论(0) 推荐(0)

Basic Core Data(Chapter 11 of Cocoa Programming for Mac OS X)
摘要:Apple decided to make this type of application extremely easy to write: NSArrayControllerwill hold on to an array of objects. Bindings will eliminate much of the glue code that would be necessary to keep the model objects in sync with the views. NSManagedObjectContextwill observe the instance v 阅读全文

posted @ 2011-02-16 16:25 Ray Z 阅读(182) 评论(0) 推荐(0)

Creating Links and Cross-References(Chapter 6 of XSLT 2nd Edition)
摘要:The key() function returns a node-set from the document, using the index specified by an <xsl:key> element.cdcatalog.xmlCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--1<?xmlversion="1.0"encoding="ISO-8859-1"?>2<!--EditedbyXMLSpy® 阅读全文

posted @ 2011-02-15 18:37 Ray Z 阅读(201) 评论(0) 推荐(0)

Archiving(Chapter 10 of Cocoa Programming for Mac OS X)
摘要:One protocol is NSCoding. If your class implements NSCoding, it promises to implement the following methods: An NSCoderis an abstraction of a stream of bytes. You can write your data to a coder or read your data from a coder. TheinitWithCoder:method in your object will read data from the coder a 阅读全文

posted @ 2011-02-15 18:01 Ray Z 阅读(253) 评论(0) 推荐(0)

NSUndoManager(Chapter 9 of Cocoa Programming for Mac OS X)
摘要:DocumentCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--1//2//MyDocument.m3//RaiseMan4//5//Createdbyb1mobileon2/14/11.6//Copyright2011__MyCompanyName__.Allrightsreserved.7//89#import"MyDocument.h"10#import"Person.h"1112@implementationMyDocument1314-( 阅读全文

posted @ 2011-02-15 11:15 Ray Z 阅读(378) 评论(0) 推荐(0)

Item 24: Express Callbacks with Delegates(Effective C#)
摘要:Delegates provide the best way to utilize callbacks at runtime, with simplerrequirements on client classes. You can configure delegate targets at runtime.You can support multiple client targets. Client callbacks should beimplemented using delegates in .NET. 阅读全文

posted @ 2011-02-14 21:10 Ray Z 阅读(157) 评论(0) 推荐(0)

Item 23: Understand How Interface Methods Differ from Virtual Methods(Effective C#)
摘要:codeCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Text;56namespaceEffectiveCSharpItem237{8interfaceIMsg9{10voidMessage();11}1213publicclassMyClass:IMsg14{15publicvoidMessa 阅读全文

posted @ 2011-02-14 20:30 Ray Z 阅读(163) 评论(0) 推荐(0)

NSArrayController(Chapter 8 of Cocoa Programming for Mac OS X)
摘要:PersonCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->1#import"Person.h"234@implementationPerson56-(id)init7{8[superinit];9expectedRaise=5.0;10personName=@"NewPerson";11returnself;12}1314-(void)dealloc15{16[personNamerelease];1 阅读全文

posted @ 2011-02-14 17:48 Ray Z 阅读(432) 评论(0) 推荐(0)

Item 22: Prefer Defining and Implementing Interfaces to Inheritance(Effective C#)
摘要:Base classes describe and implement common behaviors across relatedconcrete types. Interfaces describe atomic pieces of functionality that unrelatedconcrete types can implement. Both have their place. Classes definethe types you create. Interfaces describe the behavior of those types aspieces of fun 阅读全文

posted @ 2011-02-12 22:33 Ray Z 阅读(167) 评论(0) 推荐(0)

Getting Started(Chapter 1 of Python 2.6 Text Processing)
摘要:codeCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->1importsys2importstring34CHAR_MAP=dict(zip(string.ascii_lowercase,string.ascii_lowercase[13:26]+5string.ascii_lowercase[0:13]))67defrotate13_letter(letter):8"""9Returnthe13-charrot 阅读全文

posted @ 2011-02-11 23:26 Ray Z 阅读(194) 评论(0) 推荐(0)

Item 21: Limit Visibility of Your Types(Effective C#)
摘要:Those classes and interfaces that you expose publicly to the outside worldare your contract: You must live up to them. The more cluttered that interfaceis, the more constrained your future direction is. The fewer publictypes you expose, the more options you have to extend and modify anyimplementatio 阅读全文

posted @ 2011-02-11 21:08 Ray Z 阅读(203) 评论(0) 推荐(0)

Item 20: Prefer Immutable Atomic Value Types(Effective C#)
摘要:Immutable types are simpler to code and easier to maintain. Don&#8217;t blindly create get and set accessors for every property in your type. Your first choice for types that store data should be immutable, atomic value types. You easily can build more complicated structures from these entities. 阅读全文

posted @ 2011-02-10 21:24 Ray Z 阅读(137) 评论(0) 推荐(0)

Branching and Control Elements(Chapter 5 of XSLT 2nd Edition)
摘要:To invoke a template by name, two things have to happen: &#8226; The template you want to invoke has to have a name. &#8226; You use the &lt;xsl:call-template&gt; element to invoke the named template. The XSLT &lt;xsl:template&gt; element has a mode attribute that lets you pr 阅读全文

posted @ 2011-02-10 20:47 Ray Z 阅读(183) 评论(0) 推荐(0)

Key-Value Coding, Key-Value observing(Chapter 7 of Cocoa Programming for Mac OS X)
摘要:[代码]AppController.mCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->1#import"AppController.h"23@implementationAppController45-(id)init6{7[superinit];8[selfsetValue:[NSNumbernumberWithInt:5]forKey:@"fido"];9NSNumber*n=[selfvalueF 阅读全文

posted @ 2011-02-10 10:33 Ray Z 阅读(205) 评论(0) 推荐(0)

Item 19: Ensure That 0 Is a Valid State for Value Types(Effective C#)
摘要:The system initializes all instances of value types to 0. There is no way toprevent users from creating instances of value types that are all 0s. If possible,make the all 0 case the natural default. As a special case, enums usedas flags should ensure that 0 is the absence of all flags. 阅读全文

posted @ 2011-02-09 21:22 Ray Z 阅读(139) 评论(0) 推荐(0)

Item 16: Avoid Creating Unnecessary Objects(Effective C#)
摘要:The Garbage Collector does an efficient job of managing the memory thatyour application uses. But remember that creating and destroying heapobjects still takes time. Avoid creating excessive objects; don&#8217;t create whatyou don&#8217;t need. Also avoid creating multiple objects of referen 阅读全文

posted @ 2011-02-09 21:05 Ray Z 阅读(140) 评论(0) 推荐(0)

导航