组织费用应用程序数据的最佳方法是什么?

时间:2016-01-10 15:07:57

标签: ios iphone database data-structures code-organization

所以我正在尝试创建一个费用应用,我希望能够以多种方式对这些数据进行排序:

目前我有一个NSObject Expense来保存有关每项费用的所有信息(费用,花费的日期,功能,类别,子类别,备注,地点和收据图片)

enum Function: String{
    case Personal, Social, Work, Family
}
enum Category: String{
    case General, Personal, House, Food, Transport, Clothes, Fun, Misc
}
enum Subcategory: String{   
    case /*General*/ None, 
        /*Personal*/ Mobile, Medical, Taxes, Insurance, PersonalCare, Gadgets, Pets, Education, Laundry, Fitness, Loan, Vouchers, Subscriptions, 
        /*House*/ Groceries, Rent, Phone, Electricity, Internet, Cable, Water, Repairs, Plants, Mortgage, Electronics, Furniture, Heating,
        /*Food*/ Takeout, Fastfood, DiningOut, Cafe, Drinks, 
        /*Transport*/ Gas, Maintenance, PublicTransport, Taxi, CarLoan, Penalty, Flight, Parking, CarRentals, 
        /*Clothes*/ Shoes, Clothes, Accessories, Underwear, Bags, 
        /*Fun*/ Events, Movies, Recreation, Cultural, Sports, Books, Magazines, Music, Apps, Software, TVShows, 
        /*Misc*/ Gift, Office, Charity, Lodging, Service, Toy
}

class Expense: NSObject {
    var cost: Double
    var date: NSDateComponents

    var function: Function
    var category: Category
    var subcategory: Subcategory

    var note: String
    var location: String
    var receipt: UIImage


    init(amountSpent: Double, date: NSDate){
        self.cost = amountSpent
        //set date
        let calendar = NSCalendar.currentCalendar()
        self.date = calendar.components([NSCalendarUnit.Day, NSCalendarUnit.Month, NSCalendarUnit.Year, NSCalendarUnit.WeekOfYear, NSCalendarUnit.Hour, NSCalendarUnit.Minute, NSCalendarUnit.Second, NSCalendarUnit.Nanosecond], fromDate: date)
        //set organization
        self.function = .Personal
        self.category = .General
        self.subcategory = .None
        //set rest of attributes blank
        self.note = nil
        self.location = nil
        self.receipt = nil
    }

    convenience init(amountSpent: Double){
        self.init(amountSpent: amountSpent, date: NSDate()) //set to today's date
    }

    init(amountSpent: Double, date: NSDate, function: Function, category: Category, subcategory: Subcategory){
        self.cost = amountSpent
        //set date
        let calendar = NSCalendar.currentCalendar()
        self.date = calendar.components([NSCalendarUnit.Day, NSCalendarUnit.Month, NSCalendarUnit.Year, NSCalendarUnit.WeekOfYear, NSCalendarUnit.Hour, NSCalendarUnit.Minute, NSCalendarUnit.Second, NSCalendarUnit.Nanosecond], fromDate: date)
        //set organization
        self.function = function
        self.category = category
        self.subcategory = subcategory
        //set rest of attributes blank
        self.note = nil
        self.location = nil
        self.receipt = nil

    }
}

然而,如果我保留所有费用的数组,这不是一个理想的结构b / c,每次我想对数据进行排序时,我都必须查看所有数据。

关于如何组织我的数据/结构我的代码的任何建议将不胜感激!

0 个答案:

没有答案