读一读

NSMutableString *mStr = [NSMutableString stringWithCapacity:100];
NSLog(@"%@", mStr);
[mStr appendFormat:@"%@",@"123"];
NSLog(@"%@", mStr);
NSRange r = {0,2};
[mStr replaceCharactersInRange:r withString:@"hehe"];
NSLog(@"%@", mStr);
[mStr deleteCharactersInRange:r];
NSLog(@"%@", mStr);



NSString *some = @"abcdefg你";
NSLog(@"%@",some);

//将字符串转成大写
some = [some uppercaseString];
NSLog(@"%@",some);

//将字符串转成小写
some = [some lowercaseString];
NSLog(@"%@",some);

//获取字符串长度 8
NSLog(@"%u",[some length]);

//在字符串中查找字符串
NSRange rang = [some rangeOfString:@"c"];
if( rang.location == NSNotFound )
{
    NSLog(@"%@",@"not found");
}
else
{
    NSLog(@"%d %d",rang.location, rang.length);
}

//判断两个字符串是否相同
if( [some isEqualToString:@"abcdefg你"] )
{
    NSLog(@"%@",@"YES");
}

//字符串转数值
NSLog(@"%d",[@"123" intValue]);

//字符串是否以什么开头
if([some hasPrefix:@"abc"])
{
    NSLog(@"%@",@"YES");
}

//字符串是否以什么结尾
if([some hasSuffix:@"你"])
{
    NSLog(@"%@",@"YES");
}

//截取字符串
NSLog(@"%@", [some substringWithRange:NSMakeRange(1, 5)]);

//剔除两边空格
NSLog(@"%@", [@"  ccd  " stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]);



//常量字符串 NSString
char *cstr = "你好啊!yyds";
NSString *str1 = @"123";
NSString *str2 = [NSString stringWithFormat:@"%d",7758258];
NSString *str3 = [NSString stringWithUTF8String:cstr];
NSString *str4 = [[NSString alloc] initWithString:str1];
NSLog(@"%@ %@ %@ %@", str1, str2, str3, str4);



CGPoint p = {100, 100};//点
NSLog(@"x=%g,y=%g",p.x,p.y);

CGSize size = {100, 200};//大小
NSLog(@"width=%g,height=%g",size.width,size.height);

CGRect rect = {p, size};//矩形
NSLog(@"width=%g,height=%g",rect.size.width,rect.size.height);

NSRange range;//范围
range.location = 10;
range.length = 10;

//自定义
struct Person{
    int age;
};
struct Person person = {18};
NSLog(@"age:%d", person.age);



if( [circle isKindOfClass:Circle.class] )//判断对象是否是该类的实例
{
    NSLog(@"%@", @"YES");
}
if( [circle isMemberOfClass:Circle.class] )//判断对象是否为该类或子类的实例
{
    NSLog(@"%@", @"YES");
}



#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Circle : NSObject
{
    //@private //私有变量 类中可使用
    //@protected //保护变量 类中和子类可使用
    //@public //对象可以通过->访问
    int rad;
}
@property (nonatomic) double _width; //非线程安全属性 通过对象._width访问
@property (atomic) double _height; //线程安全属性 通过对象._height访问
@end

NS_ASSUME_NONNULL_END



//
//  Circle.h
//  Circle
//
//  Created by chicai on 2021/8/18.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Circle : NSObject
{
    int rad;
}
-(double)area;//成员方法
-(void)SetRad:(int)_rad;//成员方法
+(id)circle;//静态方法
@end

NS_ASSUME_NONNULL_END


//
//  Circle.m
//  Circle
//
//  Created by chicai on 2021/8/18.
//

#import "Circle.h"

@implementation Circle
-(void)SetRad:(int)_rad
{
    self->rad = _rad;
}
-(double)area
{
    return  3.14 * rad * rad;
}
+(id)circle
{
    return [[self class] new];
}
@end


#import <Foundation/Foundation.h>
#import "Circle.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Circle *circle = [Circle circle];
        [circle SetRad:20];
        NSLog(@"area: %f", [circle area]);
    }
    return 0;
}



在xcode中可以在项目中直接右键选择New File,再选择Cocoa Class,输入类名语言选择oc。

自动帮忙创建声明头文件和实现文件,类名.h 类型.m


//
//  SStudent.h
//  SStudent
//
//  Created by chicai on 2021/8/16.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface SStudent : NSObject
{
    NSString *name; //可以在对象中通过 self->name 访问,或直接name访问
    int age;
}
-(void)say;
//(返回类型)函数名字:(参数类型)参数名字 标签:(参数类型)参数名字
-(void)setName:(NSString *) _name andAge:(int)_age;
@end

NS_ASSUME_NONNULL_END


//
//  SStudent.m
//  SStudent
//
//  Created by chicai on 2021/8/16.
//

#import "SStudent.h"

@implementation SStudent
-(void) say
{
    NSLog(@"name:%@ age:%d",name,age);
}
-(void)setName:(NSString *)_name andAge:(int)_age
{
    name = _name;
    age = _age;
}
@end


#import <Foundation/Foundation.h>
#import "SStudent.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        SStudent *obj = [[SStudent alloc] init];//对象创建 == [SStudent new]
        [obj setName:@"haha" andAge:18];//方法调用 参数赋值
        [obj say];//方法调用
    }
    return 0;
}



NSString *a = @"hEloow";//oc的字符串
char *s = "haha";//c的字符串
NSLog(@"%@ %s", a, s);
NSString *str = [NSString stringWithFormat:@"%@ %s", a, s];
NSLog(@"%@", str);



void __NT__OpenFacebookPublicPage(const char * pageid, const char * name)
{
    NSString *str_url = [NSString stringWithFormat:@"%@%@", @"fb://profile/", __makeNSString(pageid) ];
    NSURL *facebookURL = [NSURL URLWithString:str_url];
    if ([[UIApplication sharedApplication] canOpenURL:facebookURL]) {
        [[UIApplication sharedApplication] openURL:facebookURL];
    } else {
        NSString *web_url = [NSString stringWithFormat:@"%@%@", @"https://www.facebook.com/", __makeNSString(name) ];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:web_url]];
    }
}