2011-08-03 38 views
0

我有一个方法是无效的,但我必须计算它的条目数。计数NSFetchRequest的条目数

那么有什么办法可以做到吗?

我试图实现它,但它不工作。

它给我一个Nsfetch数的错误。

返回声明为空。

testcase1

{ 
    //startdate and end date are same and both are 1 day before 
    NSTimeInterval secondsPerday=24*60*60; 
    NSDate *startdate = [[NSDate alloc]initWithTimeIntervalSinceNow:-secondsPerday]; 
    NSDate *endate = [[NSDate alloc]initWithTimeIntervalSinceNow:-secondsPerday]; 

    TravelerAppDelegate *delegate =[[UIApplication sharedApplication]delegate]; 
    [delegate getWeatherforCity:@"#" state:@"#" country:@"#" startDate:startdate endDate:endate]; 

     NSManagedObjectContext *allone=[delegate managedObjectContext]; 
     NSEntityDescription *entity = [NSEntityDescription entityForName:@"Weather" inManagedObjectContext:allone]; 
     //WeatherXMLParser *delegate = [[WeatherXMLParser alloc] initWithCity:@"#" state:@"#" country:@"#"]; 
     NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
     [request setEntity:[NSEntityDescription entityForName:@"Weather" inManagedObjectContext:allone]]; 
     [request setIncludesSubentities:NO]; 
     NSError *err; 
     NSUInteger count = [allone countForFetchRequest:request error:&err]; 
     if(count == NSNotFound) { 
      //Handle error 
     } 

     [request release]; 
     return count; 

    } 
+0

如果代码“给你一个错误“,在你的问题中包含这个错误是非常有用的。 – jrturton

回答

0

首先,定义变量实体但在设置该找取请求的实体,你正在做上述相同。简单地写:

[request setEntity:entity]; 

然后,NSFetchRequest必须有一种描述符阵列。所以,加上:

[request setSortDescriptors:[NSArray array]]; 
0

如果你想要一个方法返回一些东西,然后设置它的返回类型。

-(void)testcase1 
{ 
    // Code goes here 
} 

返回Nothing(void)。

[self testcase1]; 

返回的东西:

-(NSInteger)testcase1 
{ 
    // Code goes here 
    return count; 
} 

你会叫这样的方法是这样的:你会通过调用一个方法是这样

NSInteger count = [self testcase1];