我正在尝试统一模型文件中特定类的所有函数.例如,我在模型'Contact.h/Contact.m'中有一个函数fetchContactWithName:(NSString*)name,我的viewcontroller随后会调用它.
在这种情况下,将AppDelegate.h文件导入到模型文件中是不是一个坏主意,因为我需要访问它的managedObjectContext?
#import "AppDelegate.h"
@implementation Contact
...
+ (Contact *) fetchContactWithName:(NSString *) name {
AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:delegate.managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", name];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
NSArray *fetchedObjects = [delegate.managedObjectContext executeFetchRequest:fetchRequest error:&error];
Contact *fetchedContact;
for(Contact *contact in fetchedObjects) {
fetchedContact = contact;
}
if(fetchedContact != nil) {
return fetchedContact;
} else {
return nil;
}
}
@end
Run Code Online (Sandbox Code Playgroud) 我有以下代码,有2个部分.我希望第一部分有3个单元格,有文本,第二部分没有文字.问题是在第2节中的第6个细胞之后,细胞重复第1节中的文本.
所以我的单元格如下:第1部分 - 配置文件,联系人,设置; 第2节 - 空白,空白,空白,空白,配置文件,联系人,设置.
我究竟做错了什么?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(section == 0) return 3;
else return 9;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"LeftMenuCell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(indexPath.section == 0) {
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"Profile";
break;
case 1:
cell.textLabel.text = @"Contacts";
break;
case 2:
cell.textLabel.text = @"Settings";
break;
default: …Run Code Online (Sandbox Code Playgroud) 我正在尝试访问方法块但我不知道如何:
__block NSString *username;
PFUser *user = [[self.messageData objectAtIndex:indexPath.row] objectForKey:@"author"];
[user fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
username = [object objectForKey:@"username"];
NSLog(@"%@", username); //returns "bob";
}];
NSLog(@"%@", username); //returns null
Run Code Online (Sandbox Code Playgroud)
如何从块外部的代码访问变量'username'?
根据地图上的Golang文档,
如果请求的密钥不存在,我们将获得值类型的零值.在这种情况下,值类型为int,因此零值为0:
Run Code Online (Sandbox Code Playgroud)j := m["root"] // j == 0
所以我试图确定一个结构是否存在一个给定的字符串,我该如何确定?我会检查一个带有零值的空结构吗?这里的比较会是什么样的?
type Hello struct{}
structMap := map[string]Hello{}
j := structMap["example"]
if(j==?) {
...
}
Run Code Online (Sandbox Code Playgroud)