Objective-C入门IOS语法
.h 声明文件
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
NSString *_name;
int _age;
}
-(void)run;
-(void)eat:(NSString *)foodName;
-(int)sum:(int)num1:(int)num2;
+(NSString *)getAFuncName: (NSString *) funcName withParams:(NSString *)param;
@end
.m 实现文件
#import <Foundation/Foundation.h>
@implementation Porson
-(void)run{
NSLog(@"this is eate");
}
-(void)eat:(NSString *)foodName{
NSLog(@"主人给我的%@真好吃!",foodName);
}
-(int)sum:(int)num1:(int)num2{
int num3 = num1 + num2;
return num3;
}
-(int)sumWithNum1:(int)num1 andNum2:(int)num2{}
+(NSString *)getAFuncName: (NSString *) funcName withParams:(NSString *)param{
return @"FuncName"
}
@end
int main(int argc, const char * argv[])
{
Porson *p1 = [Porson alloc];
p1 = [p1 init];
Porson *p1 = [[Porson alloc] init];
p1->_name = @"Barry";
[p1 run]
[p1 eat:@"红烧肉"];
[p1 sumWithNum1:10 andNum2:10];
}
Student *stu= [Student new];
@property int age;
Car *car = [[[Car alloc] initWithSize:10 andPrice:23.0f] autorelease];
[person release];
@synthesize age,_no,height;
[若用synthesize了,即可在.h文件中不写成员变量age,_no,height,会默认去访问与age同名的变量,若找不到同名的变量,会自动生成一个同名变量,并且若自己定义的成员变量的名字与@synthesize不一样时,会默认创建自动生成的]
@property (nonatomic,getter=isEnable) BOOL enable;
-
读写属性:readwrite/readonly;//默认是readwrite,并生成set/get;readonly代表只生成get方法 -
setter属性:assign/retain/copy/strong;//默认是assign,直接赋值;retain,引用+1; -
原子性:atomic/nonatomic;//默认atomic,加锁,提供了多线程安全;nonatomic,禁止多线程,变量保护,可提高性能;[对于在iPhone这个小型设备上,内存很小,默认情况下不需要考虑太多的多线程,以提高性能] -
使用WKScriptMessageHandler实现js调用原生方法
[_webView.configuration.userContentController addScriptMessageHandler:self name:@"这是约定好的回调方法名"];
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
if ([message.name isEqualToString:@"约定好的方法名"]) {
NSDictionary *body = message.body;
...
}
}
window.webkit.messageHandlers.约定好的方法名.postMessage(message)
语法
NSString
NSString *stringName = [NSString stringWithFormat: @"content: %@", content];
[stringName length]| stringName.length;
unichar c = [stringName characterAtIndex:0];
StringIsEmpty(title);
if([stringName isEqualToString:@"get"]){...}
NSString *substring = [@"abcdefg" substringFromIndex: 1];
NSString *substring1 = [@"abcdefg" substringToIndex:1];
NSString *substring2 = [@"abcdefg" substringWithRange:NSMakeRange(1, 2)];
NSString *appendString = [@"abc" stringByAppendingString:@"def"];
NSString *appendString1 = [@"abc" stringByAppendingFormat:@"%d",123];
[stringname componentsSeparatedByString:@"**|**"]
NSString *contentString = @"hello world";
NSRange range = [contentString rangeOfString:@"hello"];
if (range.length != 0) {
NSString *replaceString = [contentString stringByReplacingCharactersInRange:range withString:@"hi"];
}
NSString *replaceString1 = [contentString stringByReplacingOccurrencesOfString:@"hello" withString:@"hi"];
NSInteger number = [@"123" intValue];
NSString *other = [NSString stringWithFormat:@"%ld",number];
NSString *uppercaseString = [@"abc" uppercaseString];
NSString *lowercaseString = [uppercaseString lowercaseString];
[@"abcdef" hasPrefix:@"abc"]
[@"abcdef" hasSuffix:@"def"]
NSString *hintContent = [PNCShareUtil callBackSaveStr:[dict objectForKey:@"hintContent"]];
NSString *jsPath = [[NSBundle mainBundle] pathForResource:@"tjapp" of Type:@"js"];
NSString *jsString = [NSString stringWithContentsOfFile:jsPath encoding:NSUTF8StringEncoding error:nil];
NSMutableNString
NSMutableString *string1 = [[NSMutableString alloc]initWithString:@"abcdefg"];
NSMutableString *string2 = [NSMutableString stringWithFormat:@"abcdefg"];
[string1 appendString:@"asdf"];
[string2 appendFormat:@"%d",123];
[string1 insertString:@"www." atIndex:0];
[string1 deleteCharactersInRange:NSMakeRange(1, 2)];
NSDictionary
NSDictionary *num = [[NSDictionary alloc] initWithObjectsAndKeys:@"one", @"num1", @"two", @"num2", @"three", @"num3", nil];
NSDictionary *num1 = [NSDictionary dictionaryWithObjectsAndKeys:@"one",@"num1",@"two",@"num2",nil];
NSDictionary *num2 = @{
@"num1":@"one",
@"num2":@"two"
};
NSLog(@"dict3----->%@",dict3);
NSInteger count = [dict3 count];
NSArray *arr = [dict3 allKeys];
NSArray *arr1 = [dict3 allValues];
NSString *string = [dict3 objectForKey:@"num1"];
NSMutableDictionary
NSMutableDictionary *name = [[NSMutableDictionary alloc] initWithCapacity:0];
NSMutableDictionary *name1 = [NSMutableDictionary dictionaryWithCapacity:0];
NSMutableDictionary *name2 = [@{@"key1":@"frank", @"key2":@"duck"} mutableCopy];
NSMutableDictionary *name0 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"one",@"num1",@"two",@"num2", nil];
[name2 setObject:@43 forKey:@"age"];
[name2 removeObjectForKey:@"age"];
NSLog(@"%@",name2);
[name2 removeAllObjects];
NSLog(@"%@",name2);
for (NSString *key in dict3) {
NSLog(@"%@",key);
NSLog(@"%@",dic[key]);
}
NSArray *keyArr= [dic allKeys];
for (int i=0; i<keyArr.count; i++) {
NSLog(@"%@",[dic objectForKey:keyArr[i]]);
}
NSEnumerator *enumKeys = [aDic keyEnumerator];
for (NSObject *obj in enumKeys){
NSLog(@"enumKey: %@", obj);
}
NSEnumerator *enumValues = [aDic objectEnumerator];
for (NSObject *obj in enumValues){
NSLog(@"value in dict: %@", obj);
}
-
json转化为字典 NSDictionary *params = [NSJSONSerialization JSONObjectWithData: [message.body dataUsingEncoding: NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];
-
json转化为字符串 NSString *jsonString = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:rlt options:0 error:&err]encoding:NSUTF8StringEncoding];
NSArray
NSArray *arrayname = [[NSArray alloc] initWithObjects:@"aaa", @"bbb", @"ccc", nil];
NSArray *number1 = [NSArray arrayWithObjects:@"one",@"two",@"three", nil];
NSArray *number2 = @[@"one",@"two",@"three"];
NSInteger count = [arrayname count];
NSString *arrayItem = [arrayname objectAtIndex:1];
NSUInteger loc = [arrayname indexOfObject:@"three"];
if ( [arrayname containsObject:@"one"] ){...}
NSString *string1 = [arrayname componentsJoinedByString:@"&"];
NSArray *array = [stringname componentsSeparatedByString:@"."];
NSMutableArray
NSMutableArray *name = [[NSMutableArray alloc] initWithCapacity:0];
NSMutableArray *name1 = [NSMutableArray arrayWithCapacity:0];
NSMutableArray *name2 = [@[@"frank", @"duck", @"monkey", @"cow"] mutableCopy];
[arrayname addObject:@"cat"];
[arrayname insertObject:@"dog" atIndex:1];
[arrayname removeObject:@"cat"];
[arrayname removeLastObject];
[arrayname removeAllObjects];
[arrayname removeObjectAtIndex:2];
[arrayname replaceObjectAtIndex:2 withObject:@"hhhh"];
[arrayname exchangeObjectAtIndex:1 withObjectAtIndex:2];
NSLog(@"%@", array);
for(NSInterger i = 0; i < [arrayName count]; i++){
NSLog(@"result:%@", [array objectAtIndex:i]);
}
NSEnumerator *enumerator = [array objectEnumerator];
id thingie;
while(thingie = [enumerator nextObject])
{
NSLog(@"I found %@",thingie);
}
NSString *object;
for(object in array)
{
NSLog(@"%@",object);
}
NSUserDefault
NSUserDefaults 适合存储轻量级的不需要加密的本地数据,例如用户的偏好设置、用户名等
NSString *mounttheclouds = [[NSUserDefaults standardUserDefaults] valueForKey:MOUNTTHECLOUDS];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:@"名字" forKey:@"name"];
NSString *name = [userDefaults objectForKey:@"name"];
[userDefaults removeObjectForKey:@"name"];
if([[NSUserDefaults standardUserDefaults] objectForKey:@"message"]==nil){
[[NSUserDefaults standardUserDefaults] setObject:@"This is message" forKey:@"message"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
常用方法
dispatch_async
? 为了避免界面在处理耗时的操作时卡死,比如读取网络数据,IO,数据库读写等,我们会在另外一个线程中处理这些操作,然后通知主线程更新界面。
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"main queue");
self.label = [UILabel new];
});
功能
网页
JSbridgeRouter.m, 提供RN的向原生请求跳转等方法
jumpWebview
jumpToRN
自定义request请求
NSURL *url1 = [NSURL URLWithString:@"http://getFIlehtmlConfig.xx?qq=qqq"];
NSURLRequest *request = [NSURLRequest requestWithURL:url1 cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:60];
蓝色浮窗配置
IPConfigViewController.xib .m .h
工具方法封装
[PNCShareUtil callBackSaveStr: @""];
[PNCShareUtil returnNilOrEmpty: @""];
快捷键
格式化:crtl+i
按文件名查找: commnad+shfit+o
找到当前文件在导航栏位置: command+shift+j
|