简体   繁体   English

Mac和iPhone上的共享模型

[英]Shared Model on Mac and iPhone

I'm currently looking into unifying the model of my application. 我目前正在考虑统一我的应用程序模型。 At the moment I'm using a different model on the mac/iphone. 目前我在mac / iphone上使用了不同的型号。 Missing classes (NSAttributedString) and missing technologies (Bindings) made me go with this decision. 缺少类(NSAttributedString)和缺少技术(Bindings)使我决定采用这个决定。

With the first restriction gone in SDK 3.2 and my plan to also create an optimized iPad version I'm reconsidering my decision. 随着SDK 3.2中的第一个限制以及我计划创建优化的iPad版本,我正在重新考虑我的决定。 As I also need to store NSPoints/CGPoints, NSRect/CGRects, NSColor/UIColor and NSImage/UIImage structs/objects in my model I'm not sure what the best way to handle them would be. 因为我还需要在我的模型中存储NSPoints / CGPoints,NSRect / CGRects,NSColor / UIColor和NSImage / UIImage结构/对象,我不确定处理它们的最佳方法是什么。

Writing my own MNColor object that encapsulates NSColor and UIColor depending on architecture? 编写我自己的MNColor对象,根据架构封装NSColor和UIColor? Writing my own rect-functions that call the appropriate function depending on arch? 编写我自己的rect函数,根据arch调用相应的函数? Or use CGRect in the model on the mac? 或者在mac上的模型中使用CGRect?

Any input would be very appreciated! 任何输入将非常感谢!

CorePlot is an excellent Cocoa plotting framework for iPhone and Mac OS X. CorePlot是适用于iPhone和Mac OS X的优秀Cocoa绘图框架。
It shares common code across both platforms - Maybe you can get some ideas by browsing their source . 它在两个平台上共享通用代码 - 也许你可以通过浏览它们的来源获得一些想法。

For some cross-plattform issues, CP uses platform specific defines in separate header files to get an "agnostic" image class. 对于某些跨平台问题,CP在单独的头文件中使用特定于平台的定义来获得“不可知”的图像类。
The Xcode project for each platform includes one of the according headers: 每个平台的Xcode项目包括一个相应的标题:

They also have a custom color class CPColor . 它们还有自定义颜色类CPColor (Based on CGColorRef ) (基于CGColorRef

For NSPoint and NSRect , I would use the Core Graphics structs in the model and convert them using NSRectFromCGRect and NSPointFromCGPoint where needed. 对于NSPointNSRect ,我将在模型中使用Core Graphics结构,并在需要时使用NSRectFromCGRectNSPointFromCGPoint进行转换。 (Mac OS 10.5+) (Mac OS 10.5+)

A recent CIMGF article also deals with iPhone/Mac incompatibilities: 最近的CIMGF文章还涉及iPhone / Mac不兼容性:
Creating a NSManagedObject that is Cross Platform 创建跨平台的NSManagedObject

I might be misunderstanding your setup and question but it sounds like you have a insufficiently abstracted data model. 我可能会误解你的设置和问题,但听起来你的数据模型不够抽象。

Strictly speaking, "NSPoints/CGPoints, NSRect/CGRects, NSColor/UIColor and NSImage/UIImage structs/objects" are all implementation/UI elements that have nothing to do with the data model. 严格地说,“NSPoints / CGPoints,NSRect / CGRects,NSColor / UIColor和NSImage / UIImage结构/对象”都是与数据模型无关的实现/ UI元素。 Granted, the API makes it easy to archive these but this lures you into the problem you have now. 当然,API可以轻松归档这些内容,但这会让您陷入现在的问题。 You're saving objects/structs that are attached to specific hardware and specific implementations and now you can port/reuse them easily. 您正在保存附加到特定硬件和特定实现的对象/结构,现在您可以轻松地移植/重用它们。

The better way is to create an abstracted data model that knows nothing about the hardware or the rest of the API. 更好的方法是创建一个抽象的数据模型,该模型对硬件或API的其余部分一无所知。 It should store all the NSPoints/CGPoints, NSRect/CGRects as strings or numbers. 它应该将所有NSPoints / CGPoints,NSRect / CGRects存储为字符串或数字。 It should store the colors as numbers, strings or raw data. 它应该将颜色存储为数字,字符串或原始数据。 The images should be stored as raw data. 图像应存储为原始数据。

This way the core of your application ie the data it actual manipulates is generic. 这样,应用程序的核心即它实际操作的数据是通用的。 To display the information, you just need your controller to request the raw data and let the controller convert it the hardware/API specific struct/object. 要显示信息,您只需要控制器请求原始数据,然后让控制器将其转换为硬件/ API特定的结构/对象。

Core data provides a good example of an abstracted data model. 核心数据提供了抽象数据模型的一个很好的例子。 It only stores strings, numbers, dates, booleans etc yet it can store any information of arbitrary complexity for any platform that supports core data. 它只存储字符串,数字,日期,布尔等,但它可以存储任何支持核心数据的平台的任意复杂信息。

Even if you don't use Core Data, that is the type of data model you should shoot for. 即使您不使用Core Data,也就是您应该拍摄的数据模型类型。

@"Writing my own rect-functions that call the appropriate function depending on arch" - This will be good. @“编写我自己的rect函数,根据arch调用相应的函数” - 这将是好的。

@"Writing my own MNColor object that encapsulates NSColor and UIColor" - Will be good provided your design of wrapper class is capable of handling MNColor object in cross-platform situations. @“编写我自己的封装NSColor和UIColor的MNColor对象” - 如果您的包装类设计能够在跨平台情况下处理MNColor对象,那将会很好。 ie The database from mac if imported to iPhone should now be capable of somehow providing UIColor object through your wrapper instead of NSColor. 即,如果导入到iPhone的mac数据库现在应该能够通过你的包装器而不是NSColor以某种方式提供UIColor对象。

It depends on your usage but I discourage storing images in a database. 这取决于您的使用情况,但我不鼓励将图像存储在数据库中。 They're better off on the filesystem with (perhaps) the paths to the images being stored in the database. 他们在文件系统上的表现更好,(可能)存储在数据库中的图像路径。

The one case where I can see any gains by having the images stored in the database is if you want one filesystem unit that you can move around that moves everything around. 通过将图像存储在数据库中我可以看到任何收益的一种情况是,如果您想要一个可以移动的文件系统单元,那么可以移动所有内容。 Though with the iPhone this isn't a likely use-case. 虽然使用iPhone这不是一个可能的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM