I'm new to Objective-C and I have gone through many topics, What are the key concepts on which I should put more emphasis for developing iPhone Apps?
我是Objective-C的新手,我经历了很多主题,我应该更加重视开发iPhone应用程序的关键概念是什么?
3 个解决方案
#1
54
There are a number of concepts which make up the basics of iOS development. There coding patterns, techniques and some general tidbits that you should know about.
有许多概念构成了iOS开发的基础知识。有编码模式,技术和一些你应该知道的一般花絮。
Coding Patterns:
编码模式:
-
Key Value Observing (KVO): Allowing one object to respond to changes of another object's properties by registering the "Observer" with the "target" object. For more on KVO, see Apple's Key-Value Observing Programming Guide.
键值观察(KVO):通过向“目标”对象注册“观察者”,允许一个对象响应另一个对象属性的更改。有关KVO的更多信息,请参阅Apple的Key-Value Observing Programming Guide。
-
Model View Controller Pattern: In the Model View Controller Pattern (MVC) objects generally fit into one of three roles. You have the Model, which is, at the most basic level, your data. (Or, more accurately, how the data is structured.) You have the View, which is what the user sees on the screen. Lastly, you have the Controller, which coordinates between the the model and the view. The controller is where your business logic usually goes. Apple has documentation on MVC as well.
模型视图控制器模式:在模型视图中,控制器模式(MVC)对象通常适合三种角色之一。您拥有模型,这是最基本的数据级别。 (或者,更准确地说,数据是如何构建的。)您拥有View,这是用户在屏幕上看到的内容。最后,您有Controller,它在模型和视图之间进行协调。控制器是您的业务逻辑通常所在的位置。 Apple也有关于MVC的文档。
-
The Singleton Pattern: Singleton classes (that's an oxymoron, "singleton classes") are classes which can only have one instance of them in an application at a time. Singletons are good for "factory classes", or objects that you won't want two of. The
UIDevice
class, for example, is a singleton class. (Your iPhone isn't an iPad and an iPhone at the same time, now is it?) In the iOS SDK, singleton classes often have a special initializer. Instead of the usual[[Class alloc] init]
, singletons often use[SingletonClass sharedInstance]
. ("shared"Instance, since the instance is "shared" across your application.) Note that Singleton classes work a little differently in regards to memory management.Singleton模式:Singleton类(这是一个矛盾的,“单例类”)是一类只能在一个应用程序中有一个实例的类。单身人士适合“工厂类”,或者你不想要两个人的物品。例如,UIDevice类是一个单例类。 (你的iPhone不是iPad和iPhone同时使用,现在是吗?)在iOS SDK中,单例类通常有一个特殊的初始化程序。单例通常使用[SingletonClass sharedInstance]而不是通常的[[Class alloc] init]。 (“共享”实例,因为实例在应用程序中“共享”。)请注意,Singleton类在内存管理方面的工作方式略有不同。
Coding Techniques:
编码技巧:
-
Delegation: Many objects in the iOS SDK have delegate objects, that respond to certain "events" of the object that they are "delegating" for. For example, you could have a
UIPickerView
(a scrolling wheel with a bunch of choices on it). When the user chooses a date, the delegate, ( a different object than the UIPickerView) will implement– pickerView:didSelectRow:inComponent:
, which will allow that object to do something in response to the action.委派:iOS SDK中的许多对象都有委托对象,这些对象响应他们“委托”的对象的某些“事件”。例如,你可以有一个UIPickerView(滚动轮上有一堆选项)。当用户选择日期时,委托(与UIPickerView不同的对象)将实现 - pickerView:didSelectRow:inComponent:,这将允许该对象执行某些操作以响应该操作。
-
Memory Management: Unlike many languages, be it Java, Javascript, or anything in between usually manage memory for you. On iOS, Objective-C does not do this. You need to keep track of all of your objects and release them when you are finished with them. The rule of thumb is that for every
alloc
,retain
,new
, andcopy
, you must have a correspondingrelease
, orautorelease
. (A note about autorelease: People often have trouble with understandingautorelease
. Generally speaking, local "autoreleased" objects are guaranteed to be around until the end of method call. No more, no less. Of course, if you retain the object elsewhere, it will still have a reference from that point.)内存管理:与许多语言不同,无论是Java,Javascript还是介于两者之间的任何东西,通常都会为您管理内存。在iOS上,Objective-C不会这样做。您需要跟踪所有对象并在完成后释放它们。经验法则是,对于每个alloc,retain,new和copy,您必须具有相应的版本或自动释放。 (关于自动释放的说明:人们经常在理解自动释放方面遇到麻烦。一般来说,本地“自动释放”对象保证在方法调用结束之前。不多也不少。当然,如果你将对象保留在别处,从那时起它仍然会有参考。)
-
ARC: With the iOS 5 SDK, Apple introduced Automatic Reference Counting. It's important to understand the basics of how this works, even if you plan on working with manual reference counting. You never know when you'll run into ARCified code that you'll need to work with.
ARC:通过iOS 5 SDK,Apple推出了自动引用计数。即使您计划使用手动引用计数,了解其工作原理也很重要。你永远不知道什么时候你会遇到你需要使用的ARCified代码。
-
Data Persistence: Many folks who are getting started also have a challenge with saving data in between launches. You have three options, depending on the type of data. You can use NSUserDefaults, the Documents Directory (or one of a few other folders in your App's directory hierarchy, or Core Data. You also use these in conjunction with each other, as they are not mutually exclusive.
数据持久性:许多开始使用的人也面临着在启动之间保存数据的挑战。您有三种选择,具体取决于数据类型。您可以使用NSUserDefaults,文档目录(或App的目录层次结构中的一些其他文件夹或Core Data。您也可以将它们相互结合使用,因为它们不是互斥的。
Basic Concepts:
基本概念:
-
IBOutlets and IBActions:
IBAction
andIBOutlet
aretypedef
s forvoid
.IBAction
methods returnvoid
and are marked asIBAction
so that Interface Builder can allow you to attach them to objects in your NIB files.IBOutlet
s are "placeholders" in code that are used to allow you to set properties, or otherwise interact with objects in your NIB files via Objective-C code.IBOutlets和IBActions:IBAction和IBOutlet是void的typedef。 IBAction方法返回void并标记为IBAction,以便Interface Builder可以允许您将它们附加到NIB文件中的对象。 IBOutlets是代码中的“占位符”,用于允许您通过Objective-C代码设置属性或以其他方式与NIB文件中的对象进行交互。
-
The @ symbol: The
@
symbol represents Objective-C constants, since Objective-C is a superset or Framework on top of C. In C, a string constant would be"My string is cool."
. In Objective-C it would be@"My string is cooler in Objective-C."
Other examples of the@
symbol being used to distinguish between C and Objective-C are keywords like@implementation
,@property
,@class
and@end
.@符号:@符号表示Objective-C常量,因为Objective-C是C之上的超集或框架。在C中,字符串常量为“My string is cool。”。在Objective-C中它将是@“我的字符串在Objective-C中更酷。”用于区分C和Objective-C的@符号的其他示例是@implementation,@ property,@ class和@end等关键字。
-
Pointers: Dave DeLong explains this in his answer, but this is something else to make sure you know as well.
指针:Dave DeLong在他的回答中解释了这一点,但这也是确保你也知道的其他事情。
Finally, I leave you with a word of advice:
最后,我给你留言:
Although you have *, and it really is a wonderful resource, know how to use the Apple Documentation. Enjoy your journey and good luck Getting Started!
虽然你有*,它确实是一个很好的资源,但知道如何使用Apple文档。享受您的旅程,祝您好运入门!
Good luck!
祝你好运!
#2
7
These aren't necessarily specific to iPhone development, but without them, you will not ever get it.
这些不一定特定于iPhone开发,但没有它们,你将永远不会得到它。
-
Pointers - know what a pointer is. Know why we need the dynamically allocated memory versus statically allocated memory. (I know this may sound trivial, but in my experience, this is the #1 thing that newcomers have the most trouble with) This is because you'll never* deal with raw objects in Objective-C. You always deal with object references. (ie, you never deal with an
NSString
, but always anNSString *
) However, there are things that look like objects, but aren't actually.NSRect
s are structs and they can be stack-allocated.NSInteger
is simply atypedef
for a primitiveint
. If you don't know what a pointer is, you'll go crazy wondering when you're supposed to put in a*
and when you're not.指针 - 知道指针是什么。知道为什么我们需要动态分配的内存与静态分配的内存。 (我知道这可能听起来微不足道,但根据我的经验,这是新手最麻烦的第一件事)这是因为你永远不会*处理Objective-C中的原始对象。你总是处理对象引用。 (即,你永远不会处理NSString,但总是处理NSString *)但是,有些东西看起来像对象,但实际上并不是。 NSRect是结构,它们可以堆栈分配。 NSInteger只是原始int的typedef。如果你不知道指针是什么,那么你会疯狂地想知道什么时候你应该放入一个*而你什么时候没有。
-
Memory Management - the iPhone does not have garbage collection. You must manually manage your memory. Accept that and move on. The rules for memory management conventions in Objective-C are trivial. Memorize them, and always remember to use them.
内存管理 - iPhone没有垃圾回收。您必须手动管理内存。接受并继续前进。 Objective-C中的内存管理约定规则是微不足道的。记住它们,并始终记得使用它们。
* the only time you'll deal with stack-allocated objects are blocks (^{ ... }
) or when you're doing something fiendishly devious.
*你唯一一次处理堆栈分配的对象就是块(^ {...}),或者你正在做一些非常狡猾的事情。
#3
2
For developing successful iPhone apps, you need to know more than the typically offered Objective C best practices. Of Objective C practices, knowing the delegation pattern and memory management rules is very important for iPhone development.
要开发成功的iPhone应用程序,您需要了解的不仅仅是通常提供的Objective C最佳实践。在Objective C实践中,了解委托模式和内存管理规则对于iPhone开发非常重要。
There are lots and lots of APIs in the Cocoa Touch framework. The more APIs you are familiar with, and have played with, the better. That alone can take a significant amount of time.
Cocoa Touch框架中有很多API。您熟悉和使用的API越多越好。仅此一点可能需要很长时间。
You should learn that an event driven framework such as Cocoa Touch mostly calls your app, not vice versa.
你应该知道像Cocoa Touch这样的事件驱动框架主要调用你的应用程序,反之亦然。
You should learn how UI design works differently on touch based devices with tiny displays. Too few developers see if their grandma (et.al.) wearing thick tri-focals can even see some of their icons. Or whether a UI control can be operated while using a device one-handed while walking around. Etc.
您应该了解UI设计在具有微型显示器的基于触摸的设备上的工作方式。很少有开发人员看到他们的祖母(et.al.)穿着厚厚的三人组甚至可以看到他们的一些偶像。或者在走动时单手使用设备时是否可以操作UI控件。等等。
You should learn how to design for constrained system. A mobile device likely does not have seemingly infinite amount of backing swap memory. So you need to learn how to measure and strongly control an app's memory footprint. And the battery on a small device will last a lot longer if your apps can leave the CPU mostly idle. So you will want to learn how to profile your algorithms and how to pick efficient ones.
您应该学习如何为受约束的系统进行设计。移动设备可能没有看似无限量的后备交换内存。因此,您需要学习如何衡量和强力控制应用程序的内存占用。如果您的应用程序可以让CPU大部分处于空闲状态,那么小型设备上的电池将会持续更长时间。因此,您需要了解如何分析算法以及如何选择有效的算法。
I suggest getting an older slower iOS device, and learning how to make apps work well under those limitations first. (I've heard that Apple used to do that internally as part of their training.)
我建议使用较旧的较慢的iOS设备,并首先学习如何使应用程序在这些限制下正常运行。 (我听说Apple曾经在内部这样做,作为他们培训的一部分。)
#1
54
There are a number of concepts which make up the basics of iOS development. There coding patterns, techniques and some general tidbits that you should know about.
有许多概念构成了iOS开发的基础知识。有编码模式,技术和一些你应该知道的一般花絮。
Coding Patterns:
编码模式:
-
Key Value Observing (KVO): Allowing one object to respond to changes of another object's properties by registering the "Observer" with the "target" object. For more on KVO, see Apple's Key-Value Observing Programming Guide.
键值观察(KVO):通过向“目标”对象注册“观察者”,允许一个对象响应另一个对象属性的更改。有关KVO的更多信息,请参阅Apple的Key-Value Observing Programming Guide。
-
Model View Controller Pattern: In the Model View Controller Pattern (MVC) objects generally fit into one of three roles. You have the Model, which is, at the most basic level, your data. (Or, more accurately, how the data is structured.) You have the View, which is what the user sees on the screen. Lastly, you have the Controller, which coordinates between the the model and the view. The controller is where your business logic usually goes. Apple has documentation on MVC as well.
模型视图控制器模式:在模型视图中,控制器模式(MVC)对象通常适合三种角色之一。您拥有模型,这是最基本的数据级别。 (或者,更准确地说,数据是如何构建的。)您拥有View,这是用户在屏幕上看到的内容。最后,您有Controller,它在模型和视图之间进行协调。控制器是您的业务逻辑通常所在的位置。 Apple也有关于MVC的文档。
-
The Singleton Pattern: Singleton classes (that's an oxymoron, "singleton classes") are classes which can only have one instance of them in an application at a time. Singletons are good for "factory classes", or objects that you won't want two of. The
UIDevice
class, for example, is a singleton class. (Your iPhone isn't an iPad and an iPhone at the same time, now is it?) In the iOS SDK, singleton classes often have a special initializer. Instead of the usual[[Class alloc] init]
, singletons often use[SingletonClass sharedInstance]
. ("shared"Instance, since the instance is "shared" across your application.) Note that Singleton classes work a little differently in regards to memory management.Singleton模式:Singleton类(这是一个矛盾的,“单例类”)是一类只能在一个应用程序中有一个实例的类。单身人士适合“工厂类”,或者你不想要两个人的物品。例如,UIDevice类是一个单例类。 (你的iPhone不是iPad和iPhone同时使用,现在是吗?)在iOS SDK中,单例类通常有一个特殊的初始化程序。单例通常使用[SingletonClass sharedInstance]而不是通常的[[Class alloc] init]。 (“共享”实例,因为实例在应用程序中“共享”。)请注意,Singleton类在内存管理方面的工作方式略有不同。
Coding Techniques:
编码技巧:
-
Delegation: Many objects in the iOS SDK have delegate objects, that respond to certain "events" of the object that they are "delegating" for. For example, you could have a
UIPickerView
(a scrolling wheel with a bunch of choices on it). When the user chooses a date, the delegate, ( a different object than the UIPickerView) will implement– pickerView:didSelectRow:inComponent:
, which will allow that object to do something in response to the action.委派:iOS SDK中的许多对象都有委托对象,这些对象响应他们“委托”的对象的某些“事件”。例如,你可以有一个UIPickerView(滚动轮上有一堆选项)。当用户选择日期时,委托(与UIPickerView不同的对象)将实现 - pickerView:didSelectRow:inComponent:,这将允许该对象执行某些操作以响应该操作。
-
Memory Management: Unlike many languages, be it Java, Javascript, or anything in between usually manage memory for you. On iOS, Objective-C does not do this. You need to keep track of all of your objects and release them when you are finished with them. The rule of thumb is that for every
alloc
,retain
,new
, andcopy
, you must have a correspondingrelease
, orautorelease
. (A note about autorelease: People often have trouble with understandingautorelease
. Generally speaking, local "autoreleased" objects are guaranteed to be around until the end of method call. No more, no less. Of course, if you retain the object elsewhere, it will still have a reference from that point.)内存管理:与许多语言不同,无论是Java,Javascript还是介于两者之间的任何东西,通常都会为您管理内存。在iOS上,Objective-C不会这样做。您需要跟踪所有对象并在完成后释放它们。经验法则是,对于每个alloc,retain,new和copy,您必须具有相应的版本或自动释放。 (关于自动释放的说明:人们经常在理解自动释放方面遇到麻烦。一般来说,本地“自动释放”对象保证在方法调用结束之前。不多也不少。当然,如果你将对象保留在别处,从那时起它仍然会有参考。)
-
ARC: With the iOS 5 SDK, Apple introduced Automatic Reference Counting. It's important to understand the basics of how this works, even if you plan on working with manual reference counting. You never know when you'll run into ARCified code that you'll need to work with.
ARC:通过iOS 5 SDK,Apple推出了自动引用计数。即使您计划使用手动引用计数,了解其工作原理也很重要。你永远不知道什么时候你会遇到你需要使用的ARCified代码。
-
Data Persistence: Many folks who are getting started also have a challenge with saving data in between launches. You have three options, depending on the type of data. You can use NSUserDefaults, the Documents Directory (or one of a few other folders in your App's directory hierarchy, or Core Data. You also use these in conjunction with each other, as they are not mutually exclusive.
数据持久性:许多开始使用的人也面临着在启动之间保存数据的挑战。您有三种选择,具体取决于数据类型。您可以使用NSUserDefaults,文档目录(或App的目录层次结构中的一些其他文件夹或Core Data。您也可以将它们相互结合使用,因为它们不是互斥的。
Basic Concepts:
基本概念:
-
IBOutlets and IBActions:
IBAction
andIBOutlet
aretypedef
s forvoid
.IBAction
methods returnvoid
and are marked asIBAction
so that Interface Builder can allow you to attach them to objects in your NIB files.IBOutlet
s are "placeholders" in code that are used to allow you to set properties, or otherwise interact with objects in your NIB files via Objective-C code.IBOutlets和IBActions:IBAction和IBOutlet是void的typedef。 IBAction方法返回void并标记为IBAction,以便Interface Builder可以允许您将它们附加到NIB文件中的对象。 IBOutlets是代码中的“占位符”,用于允许您通过Objective-C代码设置属性或以其他方式与NIB文件中的对象进行交互。
-
The @ symbol: The
@
symbol represents Objective-C constants, since Objective-C is a superset or Framework on top of C. In C, a string constant would be"My string is cool."
. In Objective-C it would be@"My string is cooler in Objective-C."
Other examples of the@
symbol being used to distinguish between C and Objective-C are keywords like@implementation
,@property
,@class
and@end
.@符号:@符号表示Objective-C常量,因为Objective-C是C之上的超集或框架。在C中,字符串常量为“My string is cool。”。在Objective-C中它将是@“我的字符串在Objective-C中更酷。”用于区分C和Objective-C的@符号的其他示例是@implementation,@ property,@ class和@end等关键字。
-
Pointers: Dave DeLong explains this in his answer, but this is something else to make sure you know as well.
指针:Dave DeLong在他的回答中解释了这一点,但这也是确保你也知道的其他事情。
Finally, I leave you with a word of advice:
最后,我给你留言:
Although you have *, and it really is a wonderful resource, know how to use the Apple Documentation. Enjoy your journey and good luck Getting Started!
虽然你有*,它确实是一个很好的资源,但知道如何使用Apple文档。享受您的旅程,祝您好运入门!
Good luck!
祝你好运!
#2
7
These aren't necessarily specific to iPhone development, but without them, you will not ever get it.
这些不一定特定于iPhone开发,但没有它们,你将永远不会得到它。
-
Pointers - know what a pointer is. Know why we need the dynamically allocated memory versus statically allocated memory. (I know this may sound trivial, but in my experience, this is the #1 thing that newcomers have the most trouble with) This is because you'll never* deal with raw objects in Objective-C. You always deal with object references. (ie, you never deal with an
NSString
, but always anNSString *
) However, there are things that look like objects, but aren't actually.NSRect
s are structs and they can be stack-allocated.NSInteger
is simply atypedef
for a primitiveint
. If you don't know what a pointer is, you'll go crazy wondering when you're supposed to put in a*
and when you're not.指针 - 知道指针是什么。知道为什么我们需要动态分配的内存与静态分配的内存。 (我知道这可能听起来微不足道,但根据我的经验,这是新手最麻烦的第一件事)这是因为你永远不会*处理Objective-C中的原始对象。你总是处理对象引用。 (即,你永远不会处理NSString,但总是处理NSString *)但是,有些东西看起来像对象,但实际上并不是。 NSRect是结构,它们可以堆栈分配。 NSInteger只是原始int的typedef。如果你不知道指针是什么,那么你会疯狂地想知道什么时候你应该放入一个*而你什么时候没有。
-
Memory Management - the iPhone does not have garbage collection. You must manually manage your memory. Accept that and move on. The rules for memory management conventions in Objective-C are trivial. Memorize them, and always remember to use them.
内存管理 - iPhone没有垃圾回收。您必须手动管理内存。接受并继续前进。 Objective-C中的内存管理约定规则是微不足道的。记住它们,并始终记得使用它们。
* the only time you'll deal with stack-allocated objects are blocks (^{ ... }
) or when you're doing something fiendishly devious.
*你唯一一次处理堆栈分配的对象就是块(^ {...}),或者你正在做一些非常狡猾的事情。
#3
2
For developing successful iPhone apps, you need to know more than the typically offered Objective C best practices. Of Objective C practices, knowing the delegation pattern and memory management rules is very important for iPhone development.
要开发成功的iPhone应用程序,您需要了解的不仅仅是通常提供的Objective C最佳实践。在Objective C实践中,了解委托模式和内存管理规则对于iPhone开发非常重要。
There are lots and lots of APIs in the Cocoa Touch framework. The more APIs you are familiar with, and have played with, the better. That alone can take a significant amount of time.
Cocoa Touch框架中有很多API。您熟悉和使用的API越多越好。仅此一点可能需要很长时间。
You should learn that an event driven framework such as Cocoa Touch mostly calls your app, not vice versa.
你应该知道像Cocoa Touch这样的事件驱动框架主要调用你的应用程序,反之亦然。
You should learn how UI design works differently on touch based devices with tiny displays. Too few developers see if their grandma (et.al.) wearing thick tri-focals can even see some of their icons. Or whether a UI control can be operated while using a device one-handed while walking around. Etc.
您应该了解UI设计在具有微型显示器的基于触摸的设备上的工作方式。很少有开发人员看到他们的祖母(et.al.)穿着厚厚的三人组甚至可以看到他们的一些偶像。或者在走动时单手使用设备时是否可以操作UI控件。等等。
You should learn how to design for constrained system. A mobile device likely does not have seemingly infinite amount of backing swap memory. So you need to learn how to measure and strongly control an app's memory footprint. And the battery on a small device will last a lot longer if your apps can leave the CPU mostly idle. So you will want to learn how to profile your algorithms and how to pick efficient ones.
您应该学习如何为受约束的系统进行设计。移动设备可能没有看似无限量的后备交换内存。因此,您需要学习如何衡量和强力控制应用程序的内存占用。如果您的应用程序可以让CPU大部分处于空闲状态,那么小型设备上的电池将会持续更长时间。因此,您需要了解如何分析算法以及如何选择有效的算法。
I suggest getting an older slower iOS device, and learning how to make apps work well under those limitations first. (I've heard that Apple used to do that internally as part of their training.)
我建议使用较旧的较慢的iOS设备,并首先学习如何使应用程序在这些限制下正常运行。 (我听说Apple曾经在内部这样做,作为他们培训的一部分。)