我有一个应用程序,其中显示内容给用户.我现在想知道用户实际查看该内容的秒数.所以在我的头文件中,我已经声明了一个
NSDate *startTime;
NSDate *endTime;
Run Code Online (Sandbox Code Playgroud)
然后在我的viewWillAppear中
startTime = [NSDate date];
Run Code Online (Sandbox Code Playgroud)
然后在我的viewWillDisappear中
endTime = [NSDate date];
NSTimeInterval secs = [endTime timeIntervalSinceDate:startTime];
NSLog(@"Seconds --------> %f", secs);
Run Code Online (Sandbox Code Playgroud)
但是,应用程序崩溃,有时会出现不同的错误.有时它是内存泄漏,有时它是NSTimeInterval的问题,有时它会在第二次返回内容后崩溃.
有任何想法来解决这个问题吗?
我正在使用Xcode 9和Swift 4.当应用程序在前台时,我开始下载多个JSON文件.然后,应用程序解析这些文件并将它们保存到CoreData.当应用程序位于前台时,这很有效.但是,如果应用程序在后台,文件仍然可以正确下载,但数据不会被解析并保存到CoreData.只有当用户返回到前台时,才会继续解析和保存数据.
我打开了后台模式 - 后台获取和远程通知.
我有大约10个类似下面的函数,它们同时处理JSON文件:
func populateStuff(json: JSONDictionary) -> Void {
let results = json["result"] as! JSONDictionary
let stuffData = results["Stuff"] as! [JSONDictionary]
let persistentContainer = getPersistentContainer()
persistentContainer.performBackgroundTask { (context) in
for stuff in stuffData {
let newStuff = Stuff(context: context)
newStuff.stuffCode = stuff["Code"] as? String
newStuff.stuffDescription = stuff["Description"] as? String
do {
try context.save()
} catch {
fatalError("Failure to save context: \(error)")
}
}
}
}
func getPersistentContainer() -> NSPersistentContainer {
let persistentContainer = NSPersistentContainer(name: "MyProjectName")
persistentContainer.loadPersistentStores …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序viewWillDisappear在我的一个类中的方法中存储值.例如,我在viewWillAppear方法中存储数据和时间,并且在方法中再次存储,viewWillDisappear因此我能够比较时间差并将其记录下来.
但是,如果用户按下设备上的主页按钮,viewWillDisappear则不运行代码.我试图在AppDelegates中调用这个特殊的方法applicationDidEnterBackground,但这会存储错误的信息,因为该viewWillDisappear方法实际上并未运行.我也尝试将它们存储在中NSUserDefaults,但是,由于viewWillDisappear方法没有运行,因此存储了错误的值.
有没有办法viewWillDisappear在用户按下主页按钮后立即为该视图运行该方法?
我有一个集合视图,并有自定义单元格,其中包含图像和标签.我已将集合视图设置如下 -
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
flowLayout.minimumLineSpacing = 150.0f;
flowLayout.minimumInteritemSpacing = 104.0f;
flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 100, 120);
_archiveCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
_archiveCollectionView.frame = CGRectMake(30, 218, _archiveCollectionView.frame.size.width - 60, _archiveCollectionView.frame.size.height - 350);
_archiveCollectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_archiveCollectionView.backgroundColor = [UIColor clearColor];
_archiveCollectionView.delegate = self;
_archiveCollectionView.dataSource = self;
[self.archiveCollectionView registerNib:[UINib nibWithNibName:@"FullArchiveEditionCell" bundle:nil] forCellWithReuseIdentifier:@"MyCell"];
[_archiveCollectionView reloadData];
[self.view addSubview:_archiveCollectionView];
Run Code Online (Sandbox Code Playgroud)
我还设置了以下方法:
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return _chosenCategoryArray.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath …Run Code Online (Sandbox Code Playgroud) 我创建了一个单例类来管理我的核心数据:
class CoreDataManager {
static let sharedManager = CoreDataManager()
private init() {}
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "MyContainer")
if Globals.profileNumber != 0 {
let alternateURL = NSPersistentContainer.defaultDirectoryURL()
let storeURL = alternateURL.appendingPathComponent("\(Globals.profileNumber)MyContainer.sqlite")
let storeDescription = NSPersistentStoreDescription(url: storeURL)
container.persistentStoreDescriptions = [storeDescription]
}
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
// MARK: - Core Data Saving support
func saveContext () {
let context …Run Code Online (Sandbox Code Playgroud)