是否可以像在原始应用程序中那样在swift的今天扩展中使用我的CoreData模型?如果是,我该如何创建NSManagedObjectContext?
我真的不知道,除了group-identifier之外,但不幸的是我不知道如何获取上下文..
过去我创建的应用程序在开头检查我想要使用CoreData然后我得到了managedObjectContext通过我的AppDelegate ..但我怎么能在扩展中做那样的想法?Apple不提供相关信息..
我在AppDelegate中编辑了这一行:
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"HTWcampus.sqlite"];
Run Code Online (Sandbox Code Playgroud)
对此(在将组包括在两个目标之后):
NSURL *storeURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.BenchR.TodayExtensionSharingDefaults"];
storeURL = [storeURL URLByAppendingPathComponent:@"HTWcampus.sqlite"];
NSLog(@"StoreURL2: %@", storeURL);
Run Code Online (Sandbox Code Playgroud)
有了这个,我的应用程序中的现有数据库就消失了(这很棒,因为我认为将数据库放在共享段中是有效的).
但是如何在扩展中创建我的上下文实例?我怎样才能访问我的NSManagedObject-subclasses?
在扩展中,我有这个代码到目前为止:
var context: NSManagedObjectContext!
override func viewDidLoad() {
super.viewDidLoad()
var storeURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.BenchR.TodayExtensionSharingDefaults")
storeURL = storeURL?.URLByAppendingPathComponent("HTWcampus.sqlite")
let modelURL = NSBundle.mainBundle().URLForResource("HTWcampus", withExtension: "momd")
let model = NSManagedObjectModel(contentsOfURL: modelURL)
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: model)
coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil, error: nil)
context = NSManagedObjectContext()
context.persistentStoreCoordinator = coordinator
}
Run Code Online (Sandbox Code Playgroud)
这是正确的吗?如果是的话,我怎样才能获得我的NSManagedObject-Subclasses?我是否必须将momd文件添加到扩展目标?如果是的话,我该怎么做?
正如我在标题中所说,我正在为iPhone编写一个应用程序,它在调试模式下运行完美,但当我将其构建为发布并通过TestFlight安装时,它会崩溃.由于崩溃日志,它可能必须使用以下行:
let path = NSBundle.mainBundle().pathForResource("PrinterList", ofType: "plist")
if path != nil {
let printerDic = NSDictionary(contentsOfFile: path!)
let printerList = NSArray(array: printerDic.allKeys)
printerNames = printerList as [String]
}
Run Code Online (Sandbox Code Playgroud)
我使用Brother的框架在没有AirPrint的情况下进行打印,但我认为这不是问题,因为应用程序在使用框架之前崩溃了.它只在我执行这些行的ViewController中崩溃.我也只需要在这个ViewController中使用框架.
我在if case语句中搜索与多个case进行模式匹配的语法.这个例子是这样的:
enum Gender {
case Male, Female, Transgender
}
let a = Gender.Male
Run Code Online (Sandbox Code Playgroud)
现在我想检查,如果是.Male或.Female.但我想避免使用开关.但是switch语句是这样的:
switch a {
case .Male, .Female:
// do something
}
Run Code Online (Sandbox Code Playgroud)
如果是这样的话可以写这个吗?我会期待这一点,但它不起作用:(
if case .Male, .Female = a {
}
Run Code Online (Sandbox Code Playgroud) 我有一个具有非常丰富的网络层的应用程序,我的Apple Watch应用程序取决于所有型号.不幸的是,该应用程序不够模块化,无法在手表应用程序中使用此图层.
我通过使用openParentApplication解决了这个问题:唤醒iPhone应用程序,执行请求并返回结果.
在watchOS 2中,这种方法已经消失,我应该使用WatchConnectivity.使用它的最佳方法是发送userInfo字典.
但是,如何唤醒iPhone应用程序来处理我的请求?要获得有关新userInfos的通知,我必须使用WCSessionDelegate,为此我需要一个WCSession对象.但是什么时候应该创造呢?以及如何唤醒应用程序?
所以我正在编写这个应用程序,它具有彩色导航栏和该栏中标题的字体,UIBarButtonItems的字体应为白色和特定字体.我在AppDelegate中用这两行来完成它.
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : UIFont(name: "SourceSansPro-Regular", size: 22), NSForegroundColorAttributeName : UIColor.whiteColor()]
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName : UIFont(name: "SourceSansPro-Regular", size: 22), NSForegroundColorAttributeName : UIColor.whiteColor()], forState: .Normal)
Run Code Online (Sandbox Code Playgroud)
但是使用Xcode 6.1我在每一行中都会出错,我真的不知道,有什么意思..
文本属性是[NSObject:AnyObject]?.这正是我写下来的.有人有解决方案吗?
对于我目前正在进行的项目,从我的方法接收的KeyPath实例获取KVC-String非常有用.简短的例子:
struct Person {
var name: String
}
let propertyCache = ["name": "something"]
func method<T>(_ keypath: KeyPath<Person, T>) -> T? {
let kvcName = keypath.kvc
return propertyCache[kvcName]
}
Run Code Online (Sandbox Code Playgroud)
这可能看起来不是很有用,但在我的项目中它是:)我在KeyPath上找到了一个名为_kvcKeyPathString
public 的属性,但是nil
每次我尝试都会返回.或者他们可能在那里使用反射?在此先感谢您的想法/解决方案!
我正在为我大学的学生写一个应用程序.此时,我想将计划中的课程导出为ics文件.我已经写了一些与csv文件一起使用的东西,但现在我想要同样的ics.问题是我的Mac上的iCal不希望这段代码中的应用程序创建的ics文件:
-(NSURL*)getFileUrl
{
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [docsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"Stundenplan-%@.ics", _MatrNr]];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
NSError *error = nil;
[[self getICSData] writeToURL:fileUrl atomically:YES];
if (error) {
NSLog(@"Error while writing to File: %@", [error localizedDescription]);
}
return fileUrl;
}
-(NSData*)getICSData
{
NSMutableString *erg = [[NSMutableString alloc] init];
NSDate *aktDatum = [NSDate date];
[erg appendString:[NSString stringWithFormat:@"BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//www.htw-dresden.de//iOS//DE\nMETHOD:PUBLISH\n"]];
for (Stunde *this in _daten) {
NSString *titel = [this.titel stringByReplacingOccurrencesOfString:@"," withString:@"\\, "];
NSString *dozent = [this.dozent stringByReplacingOccurrencesOfString:@"," withString:@"\\, …
Run Code Online (Sandbox Code Playgroud)