I'll be starting medium-sized iOS project soon, a document-centric app.
我很快就会启动一个中型的iOS项目,一个以文档为中心的应用程序。
Later, the software might get ported to Mac OS X. Of course, the whole UI will need to change; that's not what I'm asking.
之后,软件可能会被移植到Mac OS x上,当然,整个UI需要改变;这不是我想问的。
Is it possible to write the core logic in such a way that it's cross-platform (Mac/iOS only), or more easily ported? Has anybody done this before, or is the usual way to just write different code for both platforms?
是否有可能以一种跨平台(Mac/iOS)或更容易移植的方式来编写核心逻辑?以前有人这样做过吗?或者通常只是为两个平台编写不同的代码?
I'm thankful for any tips regarding this, be they about Code, Frameworks, Version Control - anything. I'd hate to realize I've painted myself into a corner only after the fact, so I want to start as well as possible.
我感谢任何关于这方面的提示,关于代码、框架、版本控制——任何事情。我不愿意识到,我只是在事后才把自己画成一个角落,所以我想要尽可能的开始。
4 个解决方案
#1
9
The first step is to adhere to the MVC pattern. Views often need rewriting between iOS and OS X as you say, but the model layer and some part of controllers can be reused.
第一步是遵循MVC模式。视图经常需要在iOS和OS X之间进行重写,但是模型层和控制器的某些部分可以被重用。
Foundation (NSArray
etc) and Core Data are shared between iOS and OS X, so it should be no problem to write a common Objective-C code which manages the model layer, such as saving the data to the disk, retrieving them from the network, etc.
基础(NSArray etc)和Core Data在iOS和OS X之间共享,所以编写一个通用的Objective-C代码来管理模型层应该没问题,比如将数据保存到磁盘,从网络中检索数据等等。
One important point is to resist the urge to write a long-ish code segment which mainly deals with the model layer directly in your view controller. That long-ish code segment can be abstracted into a method acting on the model layer by itself; then you can reuse that code in an OS X app later.
重要的一点是,要抵制编写一个长时间的代码段的冲动,该代码段主要是在视图控制器中直接处理模型层。可以将长时间的代码段抽象为在模型层上执行的方法;然后,您可以在稍后的OS X应用程序中重用该代码。
For example, suppose I create a view controller managing the document shown. Then in an action method defined in the view controller corresponding to an edit operation, I might write a code directly delving into the detail of the model layer and implement the edit operation. Then I would need to perform a rather big change to the source code when the view controller is ported to a window controller (or NSDocument
subclass). Rather, I would implement a method for that particular edit operation inside the document object itself, so that I can call that method both from the view controller in an iOS app and from the window controller in an OS X app.
例如,假设我创建了一个管理文档的视图控制器。然后,在与编辑操作相对应的视图控制器中定义的操作方法中,我可以编写一个代码,直接深入到模型层的细节,并实现编辑操作。然后,当视图控制器被移植到一个窗口控制器(或NSDocument子类)时,我需要对源代码执行一个相当大的更改。相反,我将为文档对象内部的特定编辑操作实现一个方法,这样我就可以从iOS应用程序的视图控制器和OS X应用程序的窗口控制器中调用该方法。
#2
3
Any pure logic and "model" level classes you create will, in the main, work on both iOS and Mac OS X, as long as you stick to the Cocoa classes, and SQLite/Core Data, etc. methods that are common to both systems. That said, the iOS file system is heavily sandboxed, which isn't a restriction on OS X and that's something you'll also need to deal with in a "document centric" app.
任何纯逻辑和“模型”级别的类都将在主要的操作系统中使用iOS和Mac OS X,只要你坚持使用Cocoa类和SQLite/Core Data等方法,这两种方法都是通用的。也就是说,iOS文件系统是重度沙箱的,这并不是对OS X的限制,这也是你在“文档中心”应用程序中需要处理的事情。
However, as you state the UI level classes are completely different on both systems, with iOS using the various UIKit classes in comparison to the various NS classes on OS X. As such, it's unlikely you'll be able to share much at that level.
但是,当您在两个系统上状态都是完全不同的时候,iOS会使用各种UIKit类来与OS x上的各种NS类进行比较,因此,您不太可能在这个级别上共享很多。
In terms of sharing the code between the two projects, you could create a "common" project of lower level objects, etc., add and this as a reference within the iOS and Mac OS X projects, although this may end up proving somewhat painful if things diverge (which I suspect they might). Irrespective, there's a good blog post on this topic called "Easy, Modular Code Sharing Across iPhone Apps: Static Libraries and Cross-Project References" by Clint Harris which is worth a read, although it does focus primarily on the iOS side of things.
而言,这两个项目之间共享代码,您可以创建一个“普通”项目的低级别对象,等,添加和这个作为参考在iOS和Mac OS X项目,尽管这可能最终证明有些痛苦如果事情偏离(我怀疑他们可能)。不管怎样,这个话题有一个很好的博客帖子,叫做“简单的,模块化的iPhone应用程序代码共享:静态库和跨项目引用”,这是值得一读的,尽管它主要关注的是iOS方面的东西。
#3
2
you could just write in C, then port it to ios, osx, java, android, ...
你可以只写C,然后把它移植到ios, osx, java, android,…
#4
2
I don't know much about this, the other guys probably know more, but if you ever wanted to port the project from iOS over to Mac then maybe UMEKit might help you...
我不太了解这个,其他人可能知道的更多,但是如果你想把这个项目从iOS移植到Mac,那么UMEKit可能会帮助你……
#1
9
The first step is to adhere to the MVC pattern. Views often need rewriting between iOS and OS X as you say, but the model layer and some part of controllers can be reused.
第一步是遵循MVC模式。视图经常需要在iOS和OS X之间进行重写,但是模型层和控制器的某些部分可以被重用。
Foundation (NSArray
etc) and Core Data are shared between iOS and OS X, so it should be no problem to write a common Objective-C code which manages the model layer, such as saving the data to the disk, retrieving them from the network, etc.
基础(NSArray etc)和Core Data在iOS和OS X之间共享,所以编写一个通用的Objective-C代码来管理模型层应该没问题,比如将数据保存到磁盘,从网络中检索数据等等。
One important point is to resist the urge to write a long-ish code segment which mainly deals with the model layer directly in your view controller. That long-ish code segment can be abstracted into a method acting on the model layer by itself; then you can reuse that code in an OS X app later.
重要的一点是,要抵制编写一个长时间的代码段的冲动,该代码段主要是在视图控制器中直接处理模型层。可以将长时间的代码段抽象为在模型层上执行的方法;然后,您可以在稍后的OS X应用程序中重用该代码。
For example, suppose I create a view controller managing the document shown. Then in an action method defined in the view controller corresponding to an edit operation, I might write a code directly delving into the detail of the model layer and implement the edit operation. Then I would need to perform a rather big change to the source code when the view controller is ported to a window controller (or NSDocument
subclass). Rather, I would implement a method for that particular edit operation inside the document object itself, so that I can call that method both from the view controller in an iOS app and from the window controller in an OS X app.
例如,假设我创建了一个管理文档的视图控制器。然后,在与编辑操作相对应的视图控制器中定义的操作方法中,我可以编写一个代码,直接深入到模型层的细节,并实现编辑操作。然后,当视图控制器被移植到一个窗口控制器(或NSDocument子类)时,我需要对源代码执行一个相当大的更改。相反,我将为文档对象内部的特定编辑操作实现一个方法,这样我就可以从iOS应用程序的视图控制器和OS X应用程序的窗口控制器中调用该方法。
#2
3
Any pure logic and "model" level classes you create will, in the main, work on both iOS and Mac OS X, as long as you stick to the Cocoa classes, and SQLite/Core Data, etc. methods that are common to both systems. That said, the iOS file system is heavily sandboxed, which isn't a restriction on OS X and that's something you'll also need to deal with in a "document centric" app.
任何纯逻辑和“模型”级别的类都将在主要的操作系统中使用iOS和Mac OS X,只要你坚持使用Cocoa类和SQLite/Core Data等方法,这两种方法都是通用的。也就是说,iOS文件系统是重度沙箱的,这并不是对OS X的限制,这也是你在“文档中心”应用程序中需要处理的事情。
However, as you state the UI level classes are completely different on both systems, with iOS using the various UIKit classes in comparison to the various NS classes on OS X. As such, it's unlikely you'll be able to share much at that level.
但是,当您在两个系统上状态都是完全不同的时候,iOS会使用各种UIKit类来与OS x上的各种NS类进行比较,因此,您不太可能在这个级别上共享很多。
In terms of sharing the code between the two projects, you could create a "common" project of lower level objects, etc., add and this as a reference within the iOS and Mac OS X projects, although this may end up proving somewhat painful if things diverge (which I suspect they might). Irrespective, there's a good blog post on this topic called "Easy, Modular Code Sharing Across iPhone Apps: Static Libraries and Cross-Project References" by Clint Harris which is worth a read, although it does focus primarily on the iOS side of things.
而言,这两个项目之间共享代码,您可以创建一个“普通”项目的低级别对象,等,添加和这个作为参考在iOS和Mac OS X项目,尽管这可能最终证明有些痛苦如果事情偏离(我怀疑他们可能)。不管怎样,这个话题有一个很好的博客帖子,叫做“简单的,模块化的iPhone应用程序代码共享:静态库和跨项目引用”,这是值得一读的,尽管它主要关注的是iOS方面的东西。
#3
2
you could just write in C, then port it to ios, osx, java, android, ...
你可以只写C,然后把它移植到ios, osx, java, android,…
#4
2
I don't know much about this, the other guys probably know more, but if you ever wanted to port the project from iOS over to Mac then maybe UMEKit might help you...
我不太了解这个,其他人可能知道的更多,但是如果你想把这个项目从iOS移植到Mac,那么UMEKit可能会帮助你……