小编Ane*_*eel的帖子

如何从NSDate中找到本周的开头?

我正在实现日历视图,我希望它在包含特定日期的一周开始时开始.例如.如果目标日期是2016年2月29日星期一,并且当前日历设置为星期日开始,我希望我的观点从2月28日星期日开始.

这似乎应该是直截了当的:

let calendar = NSCalendar.currentCalendar()
let firstDate = calendar.nextDateAfterDate(targetDate, 
                    matchingUnit: .Weekday,
                           value: calendar.firstWeekday,
                         options: .SearchBackwards)
Run Code Online (Sandbox Code Playgroud)

但这失败了:

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因是:'必须指定集合中的一个选项{NSCalendarMatchPreviousTimePreservingSmallerUnits,NSCalendarMatchNextTimePreservingSmallerUnits,NSCalendarMatchNextTime}.

我基本上可以得到我想要的东西:

let firstDate = calendar.nextDateAfterDate(firstDate, 
                    matchingUnit: .Weekday,
                           value: calendar.firstWeekday,
                        options: .MatchPreviousTimePreservingSmallerUnits)?
                    .dateByAddingTimeInterval(-7 * 84600)
Run Code Online (Sandbox Code Playgroud)

但这似乎是一种不好的做法,因为有时候一天中的秒数不是86400.

有没有更好的办法?

calendar date swift

13
推荐指数
3
解决办法
1万
查看次数

具有多个商店的CoreData:配置问题

我有一个iOS项目,包含一个大型的预加载数据库和一个小型用户数据库(两个CoreData SQLite存储).以前的问题建议使用配置来控制哪些实体与哪个商店一起使用.我无法让它工作.这是我一直在努力的...

- (NSManagedObjectModel *)managedObjectModel
{
    if (_managedObjectModel != nil) return _managedObjectModel;
    // set up the model for the preloaded data
    NSURL *itemURL = [[NSBundle mainBundle] URLForResource:@"FlagDB" withExtension:@"momd"];
    NSManagedObjectModel *itemModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:itemURL];
    // set up the model for the user data
    NSURL *userDataURL = [[NSBundle mainBundle] URLForResource:@"UserData" withExtension:@"momd"];
    NSManagedObjectModel *userDataModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:userDataURL];
    // merge the models
    _managedObjectModel = [NSManagedObjectModel modelByMergingModels:[NSArray arrayWithObjects:itemModel, userDataModel, nil]];
    // define configurations based on what was in each model
WRONG [_managedObjectModel setEntities:itemModel.entities …
Run Code Online (Sandbox Code Playgroud)

core-data objective-c multiple-databases ios ios5

11
推荐指数
1
解决办法
8673
查看次数

Objective-C:计算类别中对象的有效方法

我在一组类别中有一堆对象.我想知道每个类别有多少.

在另一种语言中,我会创建一个字典,然后遍历这些对象,为每个对象增加字典中的相应值.但是,因为我无法在Objective-C中的NSDictionary中存储本机数字类型,所以我不断在NSNumber和数字类型之间来回转换:

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
for (MyObject *obj in objs) {  
    NSNumber *old = [dictionary objectForKey:obj.category];
    NSNumber *new = [NSNumber numberWithInteger:1 + old.integerValue];
    [dictionary setObject:new forKey:obj.category];
}
Run Code Online (Sandbox Code Playgroud)

有没有更有效的方法来做到这一点?

algorithm dictionary objective-c

2
推荐指数
1
解决办法
146
查看次数

为什么我的应用程序在恢复后无法返回到我的详细信息视图?

我的应用程序有一个简单的组织,我在Interface Builder故事板中配置(不在代码中).有一个导航视图控制器,其根视图控制器设置为我的主视图控制器.我的主视图包含一个表格,其中单元格转到细节视图控制器.

当我在查看详细信息视图时暂停应用程序然后恢复它时,我将返回主视图,而不是详细信息视图.为什么会这样?

细节:

我在Interface Builder中为导航视图控制器,主视图控制器和详细视图控制器设置了恢复ID.我还尝试在表视图中添加Restoration ID,并使主视图控制器实现UIDataSourceModelAssociation.

我的应用程序从shouldRestoreApplicationState返回YES,主视图和详细信息视图都有encode/decodeRestorableStateWithCoder方法.

我正在使用模拟器测试暂停/恢复:我运行应用程序,导航到详细信息视图,点击主页按钮,然后单击XCode中的停止按钮.要恢复,我正在从XCode再次运行该应用程序.

我在挂起时看到以下调用:

AppDelegate shouldSaveApplicationState
MainViewController encodeRestorableStateWithCoder
DetailViewController encodeRestorableStateWithCoder
Run Code Online (Sandbox Code Playgroud)

在简历上:

AppDelegate shouldRestoreApplicationState
AppDelegate viewControllerWithRestorationIdentifierPath Navigation
AppDelegate viewControllerWithRestorationIdentifierPath Navigation/MainView
MainViewController viewDidLoad
AppDelegate viewControllerWithRestorationIdentifierPath Navigation/DetailView
MainViewController decodeRestorableStateWithCoder
Run Code Online (Sandbox Code Playgroud)

除了正在恢复的错误视图之外,还有其他奇怪之处:为什么详细信息视图的"恢复标识符路径"为"Navigation/DetailView"而不是"Navigation/MainView/DetailView"?导航视图控制器和详细视图控制器之间没有直接关系.它们在Interface Builder中的唯一连接是通过主视图中的segue.

我错误配置了什么吗?

我已经尝试将Restoration Class分配给Detail View.当调用该恢复代码时,它会失败,因为未在编码器中设置UIStateRestorationViewControllerStoryboardKey.

这是我的项目的玩具版本,它复制了这个问题:https://github.com/WanderingStar/RestorationTest

我正在尝试使用XCode版本5.0(5A1413)和iOS模拟器版本7.0(463.9.4),以防这些相关.

xcode objective-c ios state-restoration

2
推荐指数
1
解决办法
2474
查看次数