throws关键字是对Swift最大的误解.考虑以下代码:
func myUsefulFunction() throws
Run Code Online (Sandbox Code Playgroud)
我们无法真正理解它会抛出什么样的错误.我们唯一知道的是它可能会引发一些错误.了解错误可能的唯一方法是查看文档或在运行时检查错误.
但这不是针对斯威夫特的本性吗?Swift具有强大的泛型和类型系统,可以使代码具有表现力,但感觉就好像throws完全相反,因为您无法从查看函数签名中获得有关错误的任何信息.
为什么会这样?或者我错过了重要的事情并误解了这个概念?
我有一个非常奇怪的问题.我正在使用本教程:http://www.iosdevnotes.com/2011/03/uiscrollview-paging/以进行uiscrollview分页.但是当我尝试在故事板中实现它时,似乎不能在iOS 6中工作.我只是复制了代码并完成了所有必要的步骤.但它仍然无效.UIScrollView只是拒绝展示任何东西.但问题是,在我将Xcode更新到最新版本之前,它完美运行.任何人都可以建议我做错了什么?也许我忘了什么?
我开发了一个需要获得本周的应用程序.我试过这个:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *dateString = @"1-1-2011";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comp = [gregorian components:NSWeekCalendarUnit fromDate:dateFromString];
NSLog(@"%d", comp.week);
[dateFormatter release];
}
Run Code Online (Sandbox Code Playgroud)
但它告诉我52.这实际上是一个错误的结果.拜托,建议我任何想法.提前致谢.
所以我正在开发一个应用程序,其中包含一个包含单元格中图像的表视图.我从URL下载所有这些图像并将它们存储在一个数组中.我需要异步加载这些图像,所以我决定使用NSURLConnectionDataDelegate.我试过这段代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSUInteger row = [indexPath row];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[URLsArray objectAtIndex:row]]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
receicedData = [[NSMutableData alloc] init];
} else {
NSLog(@"Failed.");
}
return cell;
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我不知道该写些什么:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
Run Code Online (Sandbox Code Playgroud)
将图像设置为单元格中的imageViews.有人可以帮忙吗?
ios ×3
iphone ×1
nsdate ×1
paging ×1
swift ×1
throw ×1
try-catch ×1
uiscrollview ×1
uitableview ×1