iOS - 项目开发相关

  • 目录结构

1.主目录按照业务分类,内目录按照模块分类
2.主目录按照模块分类,内目录按照业务分类(推荐)


  • 常用第三方库

1、网络请求 AFNetworking
2、图片下载与缓存 SDWebImage
3、JSON 格式化 JSONModel
4、Flex 布局库 Yoga
5、UICollectionView 框架 IGListKit
6、日志库 CocoaLumberjack

  • 本地存数据 NSUserDefaults
//保存NSInteger
[defaults setInteger:(NSInteger) forKey:(nonnull NSString *)];
//保存BOOL
[defaults setBool:(BOOL) forKey:(nonnull NSString *)];
// 保存对象
[defaults setObject:@"用户名" forKey:kUsernameKey];

//读取NSInteger
[defaults setInteger:(NSInteger) forKey:(nonnull NSString *)];
//读取BOOL
[defaults setBool:(BOOL) forKey:(nonnull NSString *)];
// 读取对象
NSString *username = [defaults objectForKey:kUsernameKey];

//删除指定key的数据
[defaults removeObjectForKey:(nonnull NSString *)];
  • 基本
[UIScreen mainScreen].bounds.size  //  屏幕宽高
[UIColor redColor]  // 颜色
  • UILabel 使用
_titleLabel = [[UILabel alloc] init];
_titleLabel.text=@"欢迎使用";
_titleLabel.textColor = [UIColor blackColor];
_titleLabel.font = [UIFont systemFontOfSize:23];

// 第二种方式
UILabel *label = [UILabel new];
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] 
                                   initWithString:@"127"];
[text addAttribute:NSKernAttributeName 
             value:@-0.5 
             range:NSMakeRange(0, text.length)];
[label setAttributedText:text];
  • UIImage & UIImageView 使用
// 图片要放到 Assets.xcassets 里
UIImage *image= [UIImage imageNamed:@"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
  • UIButton 使用
    内部有 titleLabel,titleImage 等属性
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.backgroundColor = [UIColor clearColor];
[button1 setImage:[UIImage imageNamed:@"btng.png"] forState:UIControlStateNormal];
_button.layer setMasksToBounds:YES];
[_button.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
[button1 setTitle:@"点击" forState:UIControlStateNormal];
[button1.titleLabel setFont:[UIFont systemFontOfSize:40]];
[button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
  • UITableView

1、Controller需要实现两个 delegate ,分别是 UITableViewDelegate 和 UITableViewDataSource

// GTNewsViewController.m
@interface GTNewsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong, readwrite) UITableView *tableView;
@property (nonatomic, strong, readwrite) NSArray *dataArray;
@end

2、然后 UITableView对象的 delegate要设置为 self

_tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];

3、然后就可以实现这些delegate的一些方法。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 10;
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;              
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;   
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

// 返回指定的row 的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell";
    UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];
    if (cell == nil){
        // Create a cell to display an ingredient.
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                       reuseIdentifier:showUserInfoCellIdentifier] 
                autorelease];
    }
    // Configure the cell.
    cell.textLabel.text=@"签名";
    cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str()  encoding:NSUTF8StringEncoding];
        }

// 响应行点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        return;
    }
    else if(indexPath.section==0){
        switch (indexPath.row) {
            //聊天
            case 0:{
                [self  onTalkToFriendBtn];
            }
                break;
            default:
                break;
        }
    }
    else {
        return ;
    }
}
  • YogaKit 布局

以下两句容易忘记
1、layout.isEnabled =YES
2、[self.view.yoga applyLayoutPreservingOrigin:NO]

// 父级 view 设置垂直居中
    self.view.backgroundColor = [UIColor greenColor];
    [self.view configureLayoutWithBlock:^(YGLayout *_Nonnull layout) {
        layout.isEnabled = YES;
        layout.alignItems = YGAlignCenter;
        layout.justifyContent = YGJustifyCenter;
    }];

    // 子view
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor redColor];
    [view configureLayoutWithBlock:^(YGLayout *layout) {
        layout.isEnabled = YES;
        layout.width = YGPointValue(100);
        layout.height = YGPointValue(100);
        layout.flexDirection = YGFlexDirectionRow;
        layout.alignItems = YGAlignCenter;
        layout.justifyContent = YGJustifyCenter;
    }];
    [self.view addSubview:view];

    // 嵌套子 view
    [view addSubview:({
        UIView *view = [[UIView alloc] init];
        [view configureLayoutWithBlock:^(YGLayout *_Nonnull layout) {
            layout.isEnabled = YES;
            layout.width = YGPointValue(20);
            layout.height = YGPointValue(20);
        }];
        view.backgroundColor = [UIColor blackColor];
        view;
    })];

    [self.view.yoga applyLayoutPreservingOrigin:NO];

一些问题

  • LaunchScreen.storyboard & Main.storyboard 的区别
    注意:应用启动也是需要时间的。
    LaunchScreen 是用户点击启动图标后展示的第一个页面,这时候应用还没完成启动,这里不能有任何用户代码
    Main 是应用完成启动后展示的第一个页面,可以有用户代码
    从展示顺序来看,先 LaunchScreen 再 Main
  • Xcode11添加引导页(升级后Launch Images Source选项不见了)
    Xcode11添加引导页(升级后Launch Images Source选项不见了)
  • 第一次用 Podfile 安装第三方依赖后,需要点击 .xcworkspace 重新打开项目。注意不再是 .xcodeproj
  • 快捷键
    1.运行:command + R
    2.编译:command + B
    3.停止:command + .
    4.工程导航如图从左到右分别对应 command +1~8.
    5.快速打开/隐藏右上角实用面板
    a.command + 0 (注意是“0”不是“o”)
    b.command+option+0(zero)
    c.command + shift + Y



    6.快速查找打开类:command+ shift+ O
    7.删除一行 command+delete

  • 属性和成员变量在.h文件和.m文件区别

在.h文件中声明的属性,外部类可以通过“类实例.属性”来调用,
但在.m中声明的则不可以,获取和设置的方法,只能是通过setValue:forKey和valueForKey来实现。属于私有的,子类不可访问。

  • 属性定义的关键词

atomatic 读写安全,线程不安全
nonatomatic 速度快
strong 强引用
copy 深度复制一份
weak 若引用
readonly 只读
readwrite 可读可写
更多

  • MVC 结构

1、Model 写法

//.h声明
#import <Foundation/Foundation.h>
//第一个model
@interface NewModel : NSObject
@property(nonatomic,copy)NSString *familyName;//姓氏
@property(nonatomic,strong)NSArray *messageArray;//信息
- (instancetype)initWithDic:(NSDictionary *)dic;
@end

//第二个model
@interface NewModel2 : NSObject
@property(nonatomic,copy)NSString *name;//姓名
@property(nonatomic,copy)NSString *sex;//性别
- (instancetype)initWithDic:(NSDictionary *)dic;
@end

//.m实现
#import "NewModel.h"
// 第一个model
@implementation NewModel
/**
 *  构造
 *
 *  @param dic <#dic description#>
 *
 *  @return <#return value description#>
 */
- (instancetype)initWithDic:(NSDictionary *)dic
{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dic];
        //创建一个可变数组加载soldarray
        NSMutableArray *newArray = [NSMutableArray array];
        for (NSDictionary *dic in self.messageArray) {
            NewModel2 *model = [[NewModel2 alloc]initWithDic:dic];
            [newArray addObject:model];
        }
        self.messageArray = newArray;
    }
    return self;
}
@end

//第二个model
@implementation NewModel2
/**
 *  构造
 *
 *  @param dic <#dic description#>
 *
 *  @return <#return value description#>
 */
- (instancetype)initWithDic:(NSDictionary *)dic
{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}
@end

2、View 写法

// example.m
@interface GTDeleteCellView ()
@property (nonatomic, strong, readwrite) UIView *backgroundView;
@property (nonatomic, strong, readwrite) UIButton *deleteButton;
@property (nonatomic, copy, readwrite) dispatch_block_t deleteBlock;
@end

@implementation GTDeleteCellView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:({
            _backgroundView = [[UIView alloc] initWithFrame:self.bounds];
            _backgroundView.backgroundColor = [UIColor blackColor];
            _backgroundView.alpha  = 0.5;
            [_backgroundView addGestureRecognizer:({
                UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDeleteView)];
                tapGesture;
            })];
            _backgroundView;
        })];
        self.clipsToBounds = YES;
    }
    return self;
}
@end

注意:定义的私有属性可通过 _ 的形式访问
3、Controller 写法

//.h声明
#import <UIKit/UIKit.h>

@interface NewViewController : UIViewController
@property(nonatomic,weak)UITableView *tableView;
@property(nonatomic,strong)NSArray *foldArray;//数据
@end

//.m实现
#import "NewViewController.h"
#import "NewModel.h"
@interface NewViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation NewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //加载表
    [self loadTableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 *  数据加载
 */
-(NSArray *)foldArray
{
    if (_foldArray == nil) {
        NSString *path = [[NSBundle mainBundle]pathForResource:@"data.plist" ofType:nil];
        NSArray *oldArray = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *newArray = [NSMutableArray array];
        for (NSDictionary *dic in oldArray) {
            NewModel *model = [[NewModel alloc]initWithDic:dic];
            [newArray addObject:model];
        }
        _foldArray = newArray;
    }
    return _foldArray;
}
/**
 *  加载表的方法
 */
- (void)loadTableView
{
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    self.tableView = tableView;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.foldArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NewModel *fModel = self.foldArray[section];
    return fModel.messageArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * const ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    NewModel *fModel = self.foldArray[indexPath.section];
    NewModel2 *sModel = fModel.messageArray[indexPath.row];
    cell.textLabel.text = sModel.name;
    cell.detailTextLabel.text = sModel.sex;
    return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NewModel *fModel = self.foldArray[section];
    return fModel.familyName;
}
@end
  • 某 .a 文件找不到


    image.png
  • 某 .h 文件找不到


    image.png

    解决方式基本一样,就是在Build Settings 里设置 library search path 或者 framework search path


    image.png
  • xcode 的 scheme 文件可以改变构建 Debug 还是 Release
  • xcode 的 product-archive 可以用来打包 ipa
  • 使用 storyboard 方式,系统会自动创建一个UIWindow 窗口,并以ViewController.m 作为初始View。这也是 Xcode 新建项目的默认配置
  • 不使用 storyboard 方式则需要自己在 AppDelegate.m 的 didFinishLaunchWithOptions 方法中创建 UIWindow
  • Xcode-Xcode extensions - xcformat 格式化工具
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,636评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,890评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,680评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,766评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,665评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,045评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,515评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,182评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,334评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,274评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,319评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,002评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,599评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,675评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,917评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,309评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,885评论 2 341

推荐阅读更多精彩内容