简体   繁体   English

由于冗余数据,我需要为我的iOS应用重新组织我的解决方案

[英]I need to reorganize my solution for my iOS app due to redundant data

I realized I'm not approaching this correctly when I have 3 copies of the same object, so I need some guidance towards structuring this problem. 我意识到当我有3个相同对象的副本时,我无法正确处理此问题,因此我需要一些指导以解决该问题。 I'll do my best to explain the model at the moment. 现在,我将尽力解释该模型。 For simplicity VC = View Controller 为简单起见VC =视图控制器


Tl;dr: TL;博士:

MainVC -> MapVC -> DetailsVC <- FavoritesVC
 Tab1                              Tab2

MyClass objects consist of a 'favorite' bool flag and a unique id String
MainVC makes array of MyClass, gives array to MapVC after construction
MapVC constructs dict mapping Pins to MyClass, user selects pin, corresponding 
    MyClass sent to DetailsVC in segue
DetailsVC gets copy of MyClass object, displays its details, can mark as favorite
Since marking as favorite occurs on the copied object, MapVC doesn't realize it's
    marked as favorite

It starts with the MainVC . 它从MainVC开始。 This has a container view, which switches between a MapVC and a TableVC (we'll focus on the MapVC). 它有一个容器视图,可在MapVC和TableVC之间切换(我们将重点介绍MapVC)。 The MainVC uses an XML parser object to sort through a response and create custom MyClass objects and appends them to MainVC使用XML解析器对象对响应进行排序并创建自定义MyClass对象,并将其附加到

myList //(an array of MyClass)

The MapVC also has an array of MyClass called mapList. MapVC也有一个MyClass数组,称为mapList。 When MainVC is done updating myList, it also sets 当MainVC完成更新myList时,它也会设置

mapPage.mapList = myList

MapVC also has a dictionary that maps Pins to MyClass, so that when a user selects a pin I can see which MyClass it corresponds to. MapVC还具有将Pins映射到MyClass的字典,因此当用户选择一个Pin时,我可以看到它对应的MyClass。

The map sends the selected MyClass to the DetailsVC in a segue as such 映射按顺序将选定的MyClass发送到DetailsVC

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let dest = segue.destinationViewController as? DetailsTableViewController{
        dest.myobject = mapList[selectedRow]
        // selectedRow is correctly set elsewhere, no bugs
    }
}

The DetailsVC (which only knows about the single selected myObject) displays the passed (or rather copied) object's details. DetailsVC (仅知道单个选定的myObject)显示传递的(或复制的)对象的详细信息。 You can then mark it as a favorite, which sets the object's favorite flag to true. 然后,您可以将其标记为收藏夹,这会将对象的收藏夹标志设置为true。 To save favorites, I store a dictionary of MyClass objects that have been marked as favorite to UserDefaults. 为了保存收藏夹,我存储了MyClass对象的字典,该字典已被标记为UserDefaults的收藏夹。 I use a dictionary instead of an array so I can look up and remove myObjects when a user unfavorites them 我使用字典而不是数组,因此当用户不喜欢myObjects时,我可以查找和删除它们

var faveArray = [String : MyClass]()
    if let unarchivedObject = NSUserDefaults.standardUserDefaults().objectForKey(Constants.PreferenceKeys.favorites) as? NSData {
        faveArray = NSKeyedUnarchiver.unarchiveObjectWithData(unarchivedObject) as! [String : MyClass]
    }
    if isFavorite{
        faveArray[myobject.id] = myobject
    }
    else{
        faveArray.removeValueForKey(myobject.id)
    }
    defaults.setObject(NSKeyedArchiver.archivedDataWithRootObject(faveArray as NSDictionary), forKey: Constants.PreferenceKeys.favorites)

The FavoritesVC loads the UserDefault dictionary and shows each myObject in a table view. 收藏夹 VC加载UserDefault词典,并在表视图中显示每个myObject。 You can also select a myObject here, which will segue to the same DetailsVC where they can unfavorite/refavorite continuously (the favorite button updates instantly, so if you favorite, it turns into unfavorite). 您还可以在此处选择一个myObject,它将选择到同一个DetailsVC中,使它们可以连续取消收藏/重新收藏(收藏夹按钮会立即更新,因此,如果您喜欢,它会变成收藏夹)。

The problem is that when they unfavorite something from the FavoritesVC, it's a different copy MyClass object than the one sitting in the MapVC (same object, different copies), so when you go back to the map it still says "Unfavorite". 问题在于,当他们从收藏夹收藏夹中删除某些东西时,它是一个与MyVC对象不同的副本MyClass对象(相同的对象,不同的副本),因此当您返回到地图时,它仍然显示“不喜欢”。 The broader issue is that I have 3 of the same MyClass lists, 2 dictionaries, and 3 copies of the same MyClass object (1 in MapVC, 1 in ListVC, 1 in FavoritesVC) and I realize this isn't good 更广泛的问题是,我有3个相同的MyClass列表,2个词典和3个相同的MyClass对象的副本(MapVC中为1个,ListVC中为1个,FavoritesVC中为1个),我意识到这不好

I'm thinking of moving all of the data objects to a static class (is that even possible?) so that all controllers have access to the same MyClass array and objects. 我正在考虑将所有数据对象移动到静态类(甚至可能吗?),以便所有控制器都可以访问相同的MyClass数组和对象。 Is there a better or standard approach? 是否有更好或标准的方法?

What you want is a singleton. 您想要的是单身人士。 This is an instance of a class, that is initialized once and, when accessed, is the same instance (stored in the same block of memory, and the equivalent of a static instance). 这是一个类的实例,该实例被初始化一次,并且在被访问时是相同的实例(存储在相同的内存块中,并且等效于静态实例)。 There are multiple singleton patterns, and as I mentioned in my comment, if you want to not accept my answer and simply ask about singletons in Swift, that is okay! 有多个单例模式,正如我在评论中提到的那样,如果您不想接受我的回答,而只是问一下Swift中的单例,那就可以了! It's an open question. 这是一个悬而未决的问题。 Here is an example of the singleton pattern I use, for a MyClass, class: 这是我为MyClass使用的单例模式的示例:

private let mySingleton = MyClass()

class MyClass {

    class var shared : MyClass {
        return mySingleton
    }
}

Add properties, etc... to this class, of course. 当然,向此类添加属性等。 To access the SAME instance of this class in any VC, use the following code: 要在任何VC中访问此类的SAME实例,请使用以下代码:

let globalMyClass = MyClass.shared

That's it! 而已! Obviously, name your class and var whatever you like. 显然,您可以随意命名您的班级和var。 And again, there is another solid approach to global vars in Swift. 同样,在Swift中还有另一种可靠的全局变量方法。 Use this for now, but learn when you have time. 现在使用它,但是有时间的话请学习。 Good luck! 祝好运!

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

相关问题 iOS:我需要将用户的私钥(RSA)转移到我的应用程序。 文件共享是一个好的解决方案吗? - iOS: I need to transfer user's private key (RSA) to my App. Is file sharing a good solution? 需要选择最佳解决方案来存储我的ios数据 - Need to choose the best solution to store my ios Data 由于iOS数据存储准则问题,我的应用拒绝了Appstore - My app rejected Appstore due to iOS Data Storage Guidelines Issue 我的应用程序中需要核心数据吗? - Do i need core data in my app? 我需要为我的iOS应用程序使用Spotlight扩展程序吗? - Do I need to use a Spotlight extension for my iOS app? 我还需要在我的iOS应用程序中包含testflightlib吗? - Do I still need to include testflightlib in my iOS app? 我是否需要为我的iOS应用程序提交1024 * 1024的iTunesArtwork? - Do I need to submit a 1024*1024 iTunesArtwork for my iOS app? 由于未遵循iOS应用数据存储指南,我的应用被拒绝了 - My app got rejected due to not following iOS App Data Storage Guidelines 我需要从我的应用程序中访问IOS联系人。 - I need to access IOS contacts from within my app. 我需要在我的推特应用页面中为我的iOS应用设置回调网址? - What Callback url i need to set in my twitter app page for my iOS App?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM