1.冒泡排序
冒泡排序,必須掌握
int arr[8]={3,4,67,63,23,12,5,7,};
int n=sizeof(arr)/sizeof(int);
for (int i=0; i<n-1; i++)
{
for (int j=0; j<n-1-i;j++ )
{
if (arr[j]>arr[j+1])
{
arr[j]=arr[j]^arr[j+1];
arr[j+1]=arr[j]^arr[j+1];
arr[j]=arr[j+1]^arr[j];
}
}
}
除了冒泡排序外還有 插入排序,對比排序,這里舉例冒泡排序
2.單例
.h文件
+(instancetype)Shared;
.m文件
static id instance;
+(instancetype)allocWithZone:(struct _NSZone * )zone
{
if (instance==nil)
{
@synchronized (self)
{
if (instance==nil)
{
instance=[super allocWithZone:zone];
}
}
}
return instance;
}
+(instancetype)Shared
{
if (instance==nil)
{
instance=[[self alloc]init];
}
return instance;
}
這里是同步加鎖的方式,還有一種GCD的方式,就不舉例了。
3.多線程 這個我有一章節專門介紹了全部的多線程
4.block
由于使用block很容易造成循環引用,一旦出現循環引用的話內存就得不到釋放,因此一定要小心內存管理問題。
查詢內存管理問題解決辦法:
1.打印法
最好在基類controller下重寫dealloc,加一句打印日志,
表示類可以得到釋放。如果出現無打印信息,說明這個類一直得不到釋放,表明很有可能是使用block的地方出現循環引用了。對于block中需要引用外部
controller的屬性或者成員變量時,一定要使用弱引用,特別是成員變量像_testId這樣的,很多人都沒有使用弱引用,導致內存得不到釋放。
對于普通所創建的對象,因為現在都是ARC項目,所以記住內存管理的黃金法則就可以解決。
2.利用instrument 檢測內存泄露
在Xcode的instrument工具集可以很方便的檢測循環引用
Product—>profile
選擇 Leaks
之后點擊運行
如果出現紅色
點擊Details->Cycles&Roots
如圖
內存泄露圖.jpg
普通循環引用例子
NSMutableArray *oneArray = [NSMutableArray array];
NSMutableArray *twoArray = [NSMutableArray array];
[oneArray addObject:twoArray];
[twoArray addObject:oneArray];
block 循環引用的例子
.m文件
@property (nonatomic, copy) void(^block)();
@property (nonatomic, assign) NSInteger number;
- (void)test;
.h文件
- (void)test{
self.block = ^(){
NSLog(@"%ld",self.number);
};
//該引用解決辦法把下面的注釋打開,上面的注釋掉
// __weak typeof(self) weakSelf = self;
//
// self.block = ^(){
// NSLog(@"%ld",weakSelf.age);
// };
}
- (void)dealloc
{
NSLog(@"this is dealloc");
//如果對象被銷毀,則打印dealloc信息
}
某個運用該block的函數里面
YYGtest * Y = [[YYGtest alloc] init];
Y.number = 15;
[Y test];
5.SQLite數據庫的增刪改查操作
以下舉例
在要操作的函數里面:
#import <sqlite3.h> //包含頭文件
//獲取路徑
NSString * homePath=NSHomeDirectory();
NSString * docPath=[homePath stringByAppendingPathComponent:@"Documents"];
NSString * fileName=[docPath stringByAppendingPathComponent:@"db.splite”];
//對數據庫進行操作
//打開數據庫
sqlite3 * db;
if(sqlite3_open([fileName UTF8String], &db)!=SQLITE_OK)
{
NSLog(@"打開數據庫失敗!");
return;
}
//創建表
char * sql="create table if not exists t_user(username text primary key not null,password text not null)";
char * error;
if(sqlite3_exec(db, sql, NULL, NULL, &error)!=SQLITE_OK)
{
NSLog(@"創建表失敗!(%s)",error);
}
//增加數據
NSString * sql=[NSString stringWithFormat:@"insert into t_user(username,password) values('%03d','123')”,20];
//sql="insert into t_user(username,password) values('123','123')";
if(sqlite3_exec(db, [sql UTF8String], NULL, NULL, &error)!=SQLITE_OK)
{
NSLog(@"插入數據失敗!(%s)",error);
}
// 修改數據
sql=“update t_user set password='345' where username='123'";
if(sqlite3_exec(db, sql, NULL, NULL, &error)!=SQLITE_OK)
{
NSLog(@"修改數據失敗!(%s)",error);
}
//刪除數據
sql="delete from t_user where username='123'";
if(sqlite3_exec(db, sql, NULL, NULL, &error)!=SQLITE_OK)
{
NSLog(@"刪除失敗!(%s)",error);
}
//查詢數據
sql="select * from t_user where username='112’";
sqlite3_stmt * stmt;
if(sqlite3_prepare_v2(db, sql, -1, &stmt, NULL)==SQLITE_OK)
{
//遍歷
while(sqlite3_step(stmt)==SQLITE_ROW)//得到一行
{
const unsigned char * name=sqlite3_column_text(stmt, 0);
const unsigned char * pass=sqlite3_column_text(stmt, 1);
NSLog(@"username=%s,password=%s",name,pass);
}
}
sqlite3_finalize(stmt);
//關閉數據庫
sqlite3_close(db);