淘小二卖家助手 v1.4 上线
2011年12月09日 16:29
Happy iPhone coding
The original paper comes from: http://www.cocoadev.com/index.pl?SortUsingSelector
sortUsingSelector is an instance method of NSMutableArray
What should the structure of a function be when it will be called by sortUsingSelector:?
The method selector you pass in should specify a method that returns an NSComparisonResult? (either NSOrderedAscending?, NSOrderedSame?, or NSOrderedDescending?), just like the compare: method that everything responds to. If your NSMutableArray is full of NSStrings? or NSNumbers?, you can just do:
[myArray sortUsingSelector:@selector(compare:)];
and it will sort them appropriately because NSString and NSNumber both properly implement compare:. The idea is that every class should implement a compare: method that makes the receiver compare itself and return the right result.
sortUsingSelector: gets more interesting when you have objects that aren't simply strings or numbers. For instance, let's say I have a class Person with instance variables (and corresponding accessor methods) firstName and lastName whose objects I want to be able to sort by name (in the form "Last, First"). I can implement something like this:
- (NSComparisonResult)comparePerson:(Person *)p { return [[NSString stringWithFormat:@"%@, %@", [self lastName], [self firstName]] compare: [NSString stringWithFormat:@"%@, %@", [p lastName], [p firstName]]; }
Using this method with sortUsingSelector: will sort all Nelsons before all Smiths, and Abby Smith before Bernard Smith.
Of course, you can make things more flexible by deferring the sort order until runtime. Sometimes you may want to sort by first name instead of last name. In this case, the best thing to do is probably something like this:
- (NSComparisonResult)comparePerson:(Person *)p { return [[self stringForSorting] compare:[p stringForSorting]]; } - (NSString *)stringForSorting { if (something) // determine sorting type here return [NSString stringWithFormat:@"%@, %@", [self lastName], [self firstName]]; // else... return [NSString stringWithFormat:@"%@ %@", [self firstName], [self lastName]]; }
You would replace the "if (something)" condition with something useful; maybe check a user preference, or the state of something in the GUI.
-- JackNutting
Is this any different from sortUsingFunction? They seems just about the same, except with different paramaters
It is similar, but there is one important advantage to -sortUsingFunction:context:, it allows you to send in a "context" pointer, which allows you to build a sorting function that accepts some info to help control the sorting. This can be a pointer to anything at all, but in practice I find it most useful for sending in an NSString that will specify a method to base the sorting on.
To extend our previous example: We had set things up there so that the Person object could control the way the sorting was done (lastname or firstname). Let's say that we want that control to happen not in the Person class itself, but in the "user" of the Person (perhaps we've got a PersonListView?) or something).
To do this, we can implement a sorting function that accepts a "context" paremeter that allows us to specify the method used to determine sort order for the objects we're looking at. For example, we might like to be able do things like:
[personArray sortUsingFunction:comparePersonsUsingSelector context:@"lastName"]; [personArray sortUsingFunction:comparePersonsUsingSelector context:@"firstName"];
In this case we're passing in an NSString as a context to tell our function the name of the method that should be used for sorting. The function implementation would then be something like this:
static int comparePersonsUsingSelector(id p1, id p2, void *context) { // cast context to what we know it really is: an NSString NSString *methodName = context; SEL methodSelector = NSSelectorFromString(methodName); id value1 = objc_msgSend(p1, methodSelector); id value2 = objc_msgSend(p2, methodSelector); return [value1 compare:value2]; }
Note that in real code you'd want to have some error-checking here, for example making sure that the "context" passed into the function is really an NSString, making sure that p1 and p2 both respond to methodSelector, etc.
The extra functionality you get with -sortUsingFunction:context: could easily be provided in a method-based form by implementing something like -sortUsingSelector:context:; why Apple didn't include it is a mystery to me. Maybe as an "exercise for the reader"?
A simplification of the example above: pass a SEL as the context, instead of an NSString *. I've also used -[NSObject performSelector:] instead of calling objc_msgSend() directly.
[personArray sortUsingFunction:comparePersonsUsingSelector context:@selector(lastName)]; [personArray sortUsingFunction:comparePersonsUsingSelector context:@selector(firstName)]; static int comparePersonsUsingSelector(id p1, id p2, void *context) { // cast context to what we know it really is: a SEL SEL methodSelector = (SEL)context; id value1 = [p1 performSelector:methodSelector]; id value2 = [p2 performSelector:methodSelector]; return [value1 compare:value2]; }
-- Greg Parker
Advanced Sorting Techniques
Let's say you've got an array of animal objects. Each animal can be a Dog, Cat, Bird, or Fish. Now, you want to keep them sorted by kind, but within each group, you want them sorted by name also. Here's a sort method for doing something like this:
// example enum typedef enum { AnimalDog, AnimalCat, AnimalBird, AnimalFish } AnimalKind; // example class interface @interface Animal : NSObject { NSString *animalName; AnimalKind animalKind; } - (NSString *)name; - (AnimalKind)kind; @end - (NSComparisonResult)compare:(Animal *)otherAnimal { // comparing the same type of animal, so sort by name if ([self kindOfAnimal] == [otherAnimal kindOfAnimal]) return [[self name] caseInsensitiveCompare:[otherAnimal name]]; // we're comparing by kind of animal now. they will be sorted // by the order in which you declared the types in your enum // (Dogs first, then Cats, Birds, Fish, etc) return [[NSNumber numberWithInt:[self kindOfAnimal]] compare:[NSNumber numberWithInt:[otherAnimal kindOfAnimal]]]; }
Aarrgghh. Excessive object orientation alert. Creating two NSNumbers from your ints and then sending them a compare: message is a rather baroque way to find out the ordering of two integers.
The Original paper come from: http://www.webdesignerdepot.com/2009/05/how-to-get-started-with-iphone-dev/
The iPhone is a fantastic phenomenon. It’s a communications device, a multimedia platform and much more all rolled into one single tool. Everyone wants in on this device.
The Apple store has just passed the one billionth application download (I alone account for 3% of that…) and there is a wide array of applications from the amazingly useful to the bizarrely redundant.
With millions of iPhones out there, it makes sense to have your content, or application available on that platform, but how do you go about doing this? Where do you go to get started? And what are the steps you need to take to get there?
This article is an introduction to the various ways of getting content and applications onto the iPhone. It is by no means a full guide, but hopes to point you in the right direction and give you an overview of what is involved in the process.
The first step in writing for the iPhone is understanding how things really work on the iPhone. I think it is virtually impossible to develop for the iPhone without being a solid user for a while.
The iPhone has a certain way of doing things and if your content does not adhere to that it will stick out like a sore thumb. It is very different to what happens on a desktop.
The only means of interacting with content on the iPhone is your fingers. This dictates a lot of the way the interface works. The other major differences are that the screen is small, only one application runs at a time and there is very little opportunity to provide user help.
The iPhone uses animation extensively to provide a fluid, responsive interface that feels almost physical (as if the screen’s contents are really moving off, jumping or collapsing). You really need to get a feel for this to be able to create something that lives comfortably on the iPhone.
You could potentially use the iPhone simulator on a Mac instead of an actual iPhone or iPod Touch, but… that doesn’t really do it. The iPhone has a set of accelerometers that can sense the orientation and movement of the device. You really need to hold it and feel it.
Apple provides a wealth of information on its iPhone developer site:
http://developer.apple.com/iphone/
There are introductory videos, documents and sample code. Besides all the introductory material, a great document to start with is the iPhone user interface guidelines.
They can be found here:http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/MobileHIG/MobileHIG.pdf
I highly recommend starting out with this document. It has examples and sets you out on the voyage. Familiarize yourself with the way things are done on the iPhone and the arsenal of controls and functionality at your disposal.
I’m not going to go deeply into this. Planning on the iPhone is like planning for any other platform.
You need to be clear about what you want to achieve and explore what functionality you want to expose with your project. Strive for a solution that is clear, understandable, visually pleasing and of course… cool.
Once you know the game plan the search starts for the design. With the unique iPhone look, it is essential that you use that look in visualizing your project’s interface.
Recreating the iPhone interface for wireframe or sketch purposes is a lot of work. Fortunately people have already put in that effort and it is available for you to use.
These are collections of graphical widgets in various formats that can be used to assemble what looks like iPhone screens. You can use them to put together sketches and wireframes for your projects. Here are a few:
Part of the Yahoo UI Kit. This is an excellent resource for any kind of UI design visualization. The Yahoo! Design Stencil Kit version 1.0 is available for OmniGraffle, Visio (XML), Adobe Illustrator (PDF and SVG), and Adobe Photoshop (PNG). It is a set of graphics in different formats to be used in various applications and help you put together UI sketches.
Download here: http://developer.yahoo.com/ypatterns/wireframes/
A Photoshop file that has a fairly comprehensive library of assets, some editable
Download here: http://teehanlax.com/downloads/iPhone_GUI.psd.zip
A PDF or Photoshop based ’sketchepad’ for sketching out iPhone interfaces.
It can be downloaded here: http://labs.boulevart.be/index.php/2008/06/05/sketch-paper-for-the-mobile-designer/
And of course, there are several others floating around.
OK. So now you have an idea or some content, you thought of the game plan, you sketched out an interface that would look at home on the iPhone. What’s next? Well…there are several approaches you can take to get your project on the iPhone:
The iPhone has a remarkable web browser for a mobile device: Safari. It has a few tricks up its sleeves and does its best to present any website in a readable fashion. So… if you have a website that is up and running, you might get away with doing absolutely nothing.
Safari is able to present nearly any website in a readable way. The user can double click on any section of the web page and Safari will zoom in to a readable scale and present that page.
Things that are to be avoided for iPhone compliance are:
So if your site/app works well with Safari on the iPhone with no changes, that is your path of least resistance.
The next step up is to keep your site, but make a few adjustments, so that viewing it on an iPhone will be a better experience for your visitors.
Here some some simple tips and trick that will make your site work well for an iPhone visitor.
Now you’re talking! You are going to develop a website specifically for the iPhone. You need to learn what is possible from here http://developer.apple.com/safari/mobile.php and start putting it all together.
The idea is to build a web app that lives comfortably on the iPhone, preserves the visual style and behaviors the iPhone users are used to and takes advantage of the special features of the platform such as gestures, orientation changes, etc.
You don’t have to start from scratch. There are plenty of great resources that provide a good starting point or framework to build your iPhone:
Using the Aptana Studio iPhone template / Code view
Aptana Studio showing iPhone preview
Various sites developed specifically for the iPhone
Various sites developed specifically for the iPhone
The following options involve the Apple Developer tools. To access them you need to be a registered Apple developer. The suite of tools is collectively called Xcode. Xcode includes a number of tools, each tackles a different part of the puzzle:
Signing up is done here: http://developer.apple.com/
Dashcode is a strange beast. It’s part of the Xcode suite, but does not really interact with the other components (except for the iPhone simulator it uses to run projects you develop with it).
Dashcode is an IDE geared to building iPhone web apps. It has a number of templates you can use as a starting point for your app (Navigation based application, Tab bar based application etc) and take it from there.
There is a control library that you can use, dragging out controls onto your interface and then assigning properties and logic.
Dashcode saves its projects as a Dashcode project file, and when you are done you export the project as an html/javascript/css site for deployment.
It isn’t built for very elaborate complicated apps that have a lot of backend code, but if you have a straightforward self contained idea. There is nothing faster than Dashcode for putting it together.
The user guide to Dashcode can be found here
The Dashcode IDE, providing a library of controls a layout area and code editing section
Previewing a site developed in Dashcode on the iPhone simulator
Using all that webkit can offer along with one of the frameworks, or building your site using DashCode allows you to create something very close to a native iPhone app that is sensitive to orientation changes, uses animation for transitions and displays the iPhone UI widgets. What you will be missing is this:
To gain the full leverage of the app store and to take full advantage of all the iPhone has to offer, you need to use the iPhone SDK.
Creating an iPhone SDK app exposes the full potential of the iPhone. The SDK provides an incredibly rich collection of frameworks each responsible for a particular area of functionality.
The big picture is like this: You create an application in Xcode, build the user interface in Interface Builder and run it in the iPhone Simulator.
The main framework that you most likely will become most familiar with is Cocoa Touch. Among other things it contains the UIKit framework and the address book UI framework. It also supports windowing, events and user-interface management plus much more.
There is a lot of heavy lifting to be done here and a lot of information to be absorbed in order to take advantage of the richness the iPhone provides.
Fortunately there is tons of information, documentation, sample code and introduction videos available here: http://developer.apple.com/iphone/
The main concepts that you need to wrap your head around are:
Xcode provides many project templates that you can use as a starting point for the major categories of applications: Navigation based application, Tab Bar Application etc.
The first step to starting with SDK development is to download the SDK and install it. The SDK is a hefty 1GB download and requires registration as an Apple developer.
The second step is to figure out what’s going on and get your bearings within this environment. The introductory videos are a good place to start and get oriented.
You can find them here:
http://developer.apple.com/iphone/index.action
Xcode. The nerve center of the IDE development flow
Interface Builder. The tool you use to visually lay out the iPhone app interface
This last type is basically an SDK app with a twist. Sections of the app are actually Safari browser panes that are showing web pages.
This splits the development into the section that will be written using Xcode and objective c and the section that will be fetching information from the web and and presenting it in a browser view.
Basically Xcode will be used to create the application running on the iPhone and Dashcode will be used to build the web sections of the app. Your application is the combination of these two technologies cooperating.
A good reference for this type of app can be found in the user interface guidelines
To sum all this up, let’s look at the most important elements needed to create content for the iPhone:
Written exclusively for WDD by Etan Rozin. He’s a user interface designer and runs his own website at: www.rozin.com