我正在将ObjectiveController从Objective-C更新为Swift,并且我的原始代码出现了几个错误:
/* Line 161 read: NSString *dateString = [NSString stringWithFormat:@"%i-%i", start.month, start.day];
*/ This is from Objective-c file to call Holiday.json file
NSString *dateString = [NSString, stringWithFormat;@"%li-%li", (long)start.month, (long)start.day];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Holidays" ofType:@"json"];
NSString *holidayJSON = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
if (holidayJSON) {
NSData *jsonData = [holidayJSON dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *results = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
NSDictionary *holiday = [results objectForKey:dateString];
NSDictionary *titleDescription = [holiday objectForKey:@"holidays"];
NSLog(@"holiday %@", titleDescription);
NSString *title = [[titleDescription valueForKey:@"title"]objectAtIndex:0];
NSString *description = [[titleDescription valueForKey:@"description"]objectAtIndex:0];
NSLog(@"title: %@, description: %@", title, description);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:description delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alertView show];
}
NSLog( @"Selected %ld/%ld - %ld/%ld", (long)range.startDay.day, (long)range.startDay.month, (long)range.endDay.day, (long)range.endDay.month);
}
else {
NSLog( @"No selection" );
}
}
当尝试将其更改为Swift时,我遇到了几个错误:
// have calendar present a holiday from the JSON file for each day
let filePath: String = NSBundle.mainBundle().pathForResource("Holiday", ofType: "json") as String!
let jsonData = NSData(contentsOfFile:filePath) as NSData!
let dateString = ["%li-%li",DayView.monthstart.day] as NSString!
var holidayJSON: String = NSString(contentsOfFile: filePath, encoding: NSUTF8StringEncoding, error: nil)
if holidayJSON {
var jsonData: NSData = holidayJSON.dataUsingEncoding(NSUTF8StringEncoding)
var results: [NSObject : AnyObject] = NSJSONSerialization.JSONObjectWithData(jsonData, options: 0, error: nil)
var holiday: [NSObject : AnyObject] = results[dateString]
var titleDescription: [NSObject : AnyObject] = holiday["holidays"]
NSLog("holiday %@", titleDescription)
var title: String = titleDescription.valueForKey("title").objectAtIndex(0)
var description: String = titleDescription.valueForKey("description").objectAtIndex(0)
NSLog("title: %@, description: %@", title, description)
var alertView: UIAlertView = UIAlertView(title: title, message: description, delegate: nil, cancelButtonTitle: "Ok", otherButtonTitles: nilnil)
alertView.show()
}
NSLog("Selected %ld/%ld - %ld/%ld", range.startDay.day, range.startDay.month, range.endDay.day, range.endDay.month)
答案 0 :(得分:0)
我仍然很新编码/ stackoverflow / etc..etc ...所以当我提出这个问题时,我正在寻找关于如何解决问题的其他想法,我遇到了一个视频,说我可以使用桥接头文件在swift项目中使用objective-c。这是我当时没有想到的一个解决方案。但是我能够从更多经验中得到一些帮助,这是一个swift版本,不会出现错误:
`// Line 161 read: NSString *dateString = [NSString stringWithFormat:@"%i-%i", start.month, start.day];
// This is from Objective-c file to call Holiday.json file
func holidayForDate(date:CVDate) {
//let cal = NSCalendar.currentCalendar()
// let components = cal.components([.Month, .Day], fromDate: date)
let dateString = "\(date.month)-\(date.day)"
guard let path : String = NSBundle.mainBundle().pathForResource("Holiday", ofType: "json") else {
// TODO error handling?
return
}
guard let jsonData = NSData(contentsOfFile: path) else {
// TODO error handling?
return
}
do {
guard let dict:[String:AnyObject] = try NSJSONSerialization.JSONObjectWithData(jsonData, options: []) as? [String : AnyObject] else {
// TODO error handling
return
}
let holidays:[[String:String]] = dict[dateString] as! [[String:String]]
if let holiday = holidays.first {
let title = holiday["title"]
let description = holiday["description"]
UIAlertView(title: title, message: description, delegate: nil, cancelButtonTitle: "OK").show()
}
}
catch {
NSLog("\(error)")
}
}
} `