调用方法时属性访问导致未使用错误
Posted
技术标签:
【中文标题】调用方法时属性访问导致未使用错误【英文标题】:Property access result unused error while invoking a method 【发布时间】:2016-03-23 10:32:14 【问题描述】:我使用下面的代码来调用一个方法,
APIRequest *apiRequest = [[APIRequest alloc]init];
apiRequest.pendingShipmentDetails ;
我收到以下警告,
属性访问结果未使用的 getter 不应用于 side 效果
这是什么意思?
该方法同时被调用两次
以下是我的方法,
-(void)pendingShipmentDetails
keyChain = [[KeyChainItemWrapper alloc] initWithIdentifier:@"LoginController" accessGroup:nil];
NSString *username = [keyChain objectForKey:(__bridge id)(kSecAttrLabel)];
NSString *bodyString = [NSString stringWithFormat:@"\"method\":\"pending_shipment_details\",\"data\":\"username\":\"%@\"",username];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"url"]];
NSData *postData = [bodyString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[request setHTTPBody:postData];
[request setHTTPMethod:@"POST"];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
if ([data length] > 0 && connectionError == nil)
[self receivedPendingShipmentDetailsData:data];
NSString* pendingResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData *jsonData = [pendingResponse dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
NSDictionary *valueDict = [jsonDic objectForKey:@"value"];
NSArray *details = [valueDict objectForKey:@"details"];
NSMutableArray* arrayList = [[NSMutableArray alloc] init];
for (NSDictionary *response in details)
NSString *invoiceNo = [response valueForKey:@"invoice_no"];
NSString *shipmentNumber = [response valueForKey:@"shipment_no"];
NSString *invoiceDate = [response valueForKey:@"invoice_date"];
NSString *proformaInvoiceNumber = [response valueForKey:@"proforma_invoice_no"];
NSString *proformaInvoiceDate = [response valueForKey:@"proforma_invoice_date"];
NSString *plannedShipmentDates = [response valueForKey:@"planned_shipment_dates"];
NSString *pointOfContact =[[response objectForKey:@"point_of_contact"] valueForKey:@"empid"];
NSString *pendingStatus = [response valueForKey:@"status"];
NSDictionary *productName = [response valueForKey:@"products"];
NSDictionary *quantity = [response valueForKey:@"quantity"];
NSDictionary *rate = [response valueForKey:@"rate"];
NSDictionary *amount = [response valueForKey:@"amount"];
NSDictionary *productImages = [response valueForKey:@"product_image"];
NSDictionary *point_of_contact = [response valueForKey:@"point_of_contact"];
NSString *qty;
NSString *rte;
NSString *img;
NSString *address;
NSString *name;
NSString *amt;
for (int i = 0; i < [productName count]; i++)
// NSLog(@"product name %@",[productName valueForKey:[NSString stringWithFormat:@"%d",i ]]);
name = [productName valueForKey:[NSString stringWithFormat:@"%d",i ]];
for (int i = 0; i < [quantity count]; i++)
// NSLog(@"quantity %@",[quantity valueForKey:[NSString stringWithFormat:@"%d",i ]]);
qty = [quantity valueForKey:[NSString stringWithFormat:@"%d",i ]];
for (int i = 0; i < [rate count]; i++)
// NSLog(@"rate %@",[rate valueForKey:[NSString stringWithFormat:@"%d",i ]]);
rte = [rate valueForKey:[NSString stringWithFormat:@"%d",i ]];
for (int i = 0; i < [amount count]; i++)
// NSLog(@"amount %@",[amount valueForKey:[NSString stringWithFormat:@"%d",i ]]);
amt = [amount valueForKey:[NSString stringWithFormat:@"%d",i ]];
for (int i = 0; i < [productImages count]; i++)
// NSLog(@"product image %@",[productImages valueForKey:[NSString stringWithFormat:@"%d",i ]]);
img = [productImages valueForKey:[NSString stringWithFormat:@"%d",i ]];
for (int i = 0; i < [point_of_contact count]; i++)
// NSLog(@"pt of ct %@",[point_of_contact valueForKey:[NSString stringWithFormat:@"empid" ]]);
address = [point_of_contact valueForKey:[NSString stringWithFormat:@"empid" ]];
NSManagedObjectContext *context = [self managedObjectContext];
//update
if((self.pendingShipmentDAO) )
NSLog(@"update");
[self.pendingShipmentDAO setValue:shipmentNumber forKey:@"shipmentno"];
[self.pendingShipmentDAO setValue:proformaInvoiceNumber forKey:@"proforma_invoice_no"];
[self.pendingShipmentDAO setValue:proformaInvoiceDate forKey:@"proforma_invoice_date"];
[self.pendingShipmentDAO setValue:invoiceNo forKey:@"invoice_no"];
[self.pendingShipmentDAO setValue:invoiceDate forKey:@"invoice_date"];
[self.pendingShipmentDAO setValue:plannedShipmentDates forKey:@"planned_shipment_date"];
[self.pendingShipmentDAO setValue:address forKey:@"point_of_contact"];
[self.pendingShipmentDAO setValue:address forKey:@"empid"];
[self.pendingShipmentDAO setValue:name forKey:@"products"];
[self.pendingShipmentDAO setValue:qty forKey:@"quantity"];
[self.pendingShipmentDAO setValue:rte forKey:@"rate"];
[self.pendingShipmentDAO setValue:amt forKey:@"amount"];
[self.pendingShipmentDAO setValue:img forKey:@"product_image"];
[self.pendingShipmentDAO setValue:pendingStatus forKey:@"status"];
else
NSLog(@"create");
NSManagedObject *pendingShipment = [NSEntityDescription insertNewObjectForEntityForName:@"PendingShipmentDetails" inManagedObjectContext:context];
// [self deleteAllPreviousData];
[pendingShipment setValue:shipmentNumber forKey:@"shipmentno"];
[pendingShipment setValue:proformaInvoiceNumber forKey:@"proforma_invoice_no"];
[pendingShipment setValue:proformaInvoiceDate forKey:@"proforma_invoice_date"];
[pendingShipment setValue:invoiceNo forKey:@"invoice_no"];
[pendingShipment setValue:invoiceDate forKey:@"invoice_date"];
[pendingShipment setValue:plannedShipmentDates forKey:@"planned_shipment_date"];
[pendingShipment setValue:address forKey:@"point_of_contact"];
[pendingShipment setValue:address forKey:@"empid"];
[pendingShipment setValue:name forKey:@"products"];
[pendingShipment setValue:qty forKey:@"quantity"];
[pendingShipment setValue:rte forKey:@"rate"];
[pendingShipment setValue:amt forKey:@"amount"];
[pendingShipment setValue:img forKey:@"product_image"];
[pendingShipment setValue:pendingStatus forKey:@"status"];
else if ([data length] == 0 && connectionError == nil)
[self emptyReply];
else if (connectionError != nil && connectionError.code == NSURLErrorTimedOut)
[self timedOut];
else if (connectionError != nil)
[self downloadError:connectionError];
];
【问题讨论】:
显示类声明(@interface
位);在某处一定有一个@property SomeType *pendingShipmentDetails;
...
@interface MyOrdersController () @property (strong) NSMutableArray *devices; @end
【参考方案1】:
点符号用于属性(获取或设置)它们。所以这里编译器知道你想调用pendingShipmentDetails
的getter,但事实并非如此。您必须使用[]
来调用方法。将apiRequest.pendingShipmentDetails
替换为[apiRequest pendingShipmentDetails]
,警告应该会消失。
【讨论】:
行得通。你能帮我纠正第二个错误吗? else 循环正在执行两次 “你想调用setter”你的意思是getter
也许吗?
我实际上是在解析 json 并将这些内容添加到核心数据的实体中。然后,我创建一个 NSManagedObject 实例,如果以前不存在这样的实例。如果它存在,我试图用新获取的数据更新实体,但它没有发生。 :(
self.pendingShipmentDAO
的值在哪里设置?以上是关于调用方法时属性访问导致未使用错误的主要内容,如果未能解决你的问题,请参考以下文章
未捕获的TypeError:尝试在Vuejs中调用onchange()方法时,无法读取未定义的属性“apply”