我代表一个人的班级有一种计算该人年龄的方法:
@interface Student : NSObject
@property (strong) NSDate *dob;
// This method will work out the person's age in calendar years
- (NSDateComponents*)ageCalculation:(NSDate*)dob;
Run Code Online (Sandbox Code Playgroud)
这是类实现文件
@implementation Student
- (NSDateComponents*)ageCalculation:(NSDate*)dob {
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit units = NSYearCalendarUnit | NSMonthCalendarUnit;
NSDateComponents *components = [calendar components:units
fromDate:dob
toDate:now
options:0];
return components;
}
@end
Run Code Online (Sandbox Code Playgroud)
我不确定我是否正确行事,但是:
Student *student1 = [[Student alloc] init];
[student1 setDob:aDateOfBirth];
NSDateComponents *ageComponents = [student1 ageCalculation:aDateOfBirth];
Run Code Online (Sandbox Code Playgroud)
我应该怎么做这个方法的结果?我怎样才能ageCalculation:使用我已设定的日期?
我明白dispatch_async()在后台线程上运行一些东西,然后dispatch_main()在主线程上运行它,那么它dispatch_sync()会在哪里进来?
来自Android开发,我使用SharedPreferences存储用户名和密码以使用OutputStreamWriter和HttpURLConnection登录到服务器。现在在iOS 中我使用NSMutableURLRequest发送用户名和密码。是否有任何的iOS存储喜欢的用户名和密码SharedPreferences中的iOS?请记住,这是敏感数据,因此需要确保安全。
我们可以将C函数定义为inline.但Obj-C方法怎么样?我可以制作"内联"方法吗?
我正在检查一个字符串是否在一个数组中连续出现两次.这段代码似乎不起作用,因为它只打印出整个数组.我错过了什么?
NSString *nameString =
[NSString stringWithContentsOfFile:@"/usr/share/dict/words"
encoding:NSUTF8StringEncoding
error:NULL];
NSArray *names = [nameString componentsSeparatedByString:@"\n"];
//Save last item
NSMutableString *lastOne = [NSMutableString stringWithCapacity:20];
// Go through the array one string at a time
for (NSString *n in names) {
if ([n compare:lastOne options:NSCaseInsensitiveSearch]) {
NSLog(@"%@", n);
}
[lastOne setString:n];
}
Run Code Online (Sandbox Code Playgroud) 我收到一个编译错误,没有任何谷歌搜索或尝试解决它似乎工作.
我可以举例说明:
#import "test.h"
#define FOO 6;
@implementation test
-(void)test {
for (int i; i< FOO; i++) { // Error: Expected ')'
NSLog(@"test1");
}
if (1 < FOO) {
NSLog(@"test2");
}
}
@end
Run Code Online (Sandbox Code Playgroud)
在该-test方法中,它始终给出错误FOO.
这是在Xcode 5.0.2中使用默认设置/设置.
我尝试过产品>清洁,产品>构建.尝试将标题放在标题中.尝试将它们置于任何代码之上(上方#import).这是一个截图:

当我将一个字符串附加到NSMutableString使用时appendString:我得到以下错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'Attempt to mutate immutable object with appendString:'
Run Code Online (Sandbox Code Playgroud)
我的代码是:
self.partialWord = [NSMutableString stringWithCapacity:self.wordLength];
for (int i=0; i<wordLength; i++) {
[self.partialWord appendString:@"-"];
}
Run Code Online (Sandbox Code Playgroud)
在我的头文件中:
@property (copy, nonatomic, readwrite) NSMutableString *partialWord;
Run Code Online (Sandbox Code Playgroud)
我不明白为什么它说对象是不可变的,因为它是一个NSMutableString.为什么这[self.partialWord appendString:@"-"];部分不起作用?
我有两个列表,这是z和w下面:
z = [323, 493, 245, 542, 352, 463, 352]
w = [300, -9999, 4343, 320, -9999, -9999, -9999]
Run Code Online (Sandbox Code Playgroud)
现在,我需要w通过用-9999相应的z值替换所有值来更新值.如果我打印w,价值将是
w = [300, 493, 4343, 320, 352, 463, 352]
Run Code Online (Sandbox Code Playgroud)
这是我用代码得到的:
w[w == -9999] = z
Run Code Online (Sandbox Code Playgroud)
但我明白了
w = [[300, 493, 4343, 320, 352, 463, 352], -9999, 4343, 320, -9999, -9999, -9999]
Run Code Online (Sandbox Code Playgroud) 我需要知道[NSTimeZone timeZoneWithAbbreviation: @"GMT"]
swift中的等效语法
self.在块中使用会导致保留周期,因此我需要创建一个引用weakSelf.我理解这一点
但!
如果从我的块我调用一个使用self" 的方法,这是否也会导致保留周期?例如,如果我UITableView从一个块重新加载一个和UITableView我调用的一些委托方法self.,我是否导致保留周期?这是否意味着我必须到处传递这个弱的参考?似乎很好.
objective-c ×6
ios ×5
cocoa ×2
cocoa-touch ×2
concurrency ×1
credentials ×1
inline ×1
list ×1
macros ×1
methods ×1
nsdate ×1
nsstring ×1
nstimezone ×1
passwords ×1
python ×1
retain-cycle ×1
swift ×1
time ×1