iOS 面試之三道題

來源:互聯網
上載者:User

iOS 面試之三道題
1、定義宏實現MAX或者MIN.菜鳥的答案是這樣的:

#define MAX(X,Y) X>Y ? X : Y

驗證:當輸入MAX(1 == 3, 2)本來結果期望的是2,可實際結果卻是0?

高手的答案是這樣的:
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))

到這裡,或許大部分人都說沒有問題了,大學的時候老師都是這麼教的啊?你不能再說錯了吧。錯不錯,不能光說不練,我們舉例說明吧。
驗證程式碼片段如下:

float x = 1.5f;float y = 2.0f;float result = MAX(++x, y);printf("result=%f,x=%f", result, x);

最後x和result的值都是3.5,為什麼呢?MAX(++x, y) 展開後是這樣的((++x) > (y) ? (++x) : (y)) x自增兩次,有副作用啊!

大牛的答案是這樣的:
#define MAX(x,y) ({ \        typeof(x) _max1 = (x);\        typeof(y) _max2 = (y); \        (void) (&_max1 == &_max2); \        _max1 > _max2 ? _max1 : _max2; })

經驗證上面兩種情況,沒有問題了。
說明:這個宏的寫法其實是Linux核心MAX的實現方式。說明幾點,(1) typeof(x)的用途是得到x的類型資訊,比如typeof(10)為int, typeof(1.2f)為double, (2)(void)(&_x == &_y);這一句的作用是判斷_x和_y的類型是否一樣,如果不一樣的話,編譯器會給出警告資訊。

2、講述UITableView的重用機制?

UITableView為了節約記憶體和高效,會為同一類的cell設定一個唯一標示符,當滑動的時候如果這個cell滑出螢幕,那麼就將這個cell存起來,當有新的cell需要顯示的時候先從記憶體中找,如果找到,就直接重用,否則重新 alloc一個。那麼,接下來有2個問題:
(1)如果螢幕中間只能顯示10個cell,那麼滑動的時候第11個cell是否是重用的?
(2)UITableView還有其它部分需要重用嗎?
然後,直接上代碼吧,先是第一個問題:

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    _dataSource = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11"];    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];    _tableView.delegate = self;    _tableView.dataSource = self;    [_tableView setSeparatorColor:[UIColor redColor]];    [self.view addSubview:_tableView];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}#pragma mark - UITableViewDataSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return [_dataSource count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString *sCellReusableIdentify = @"sCellReusableIdentify";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sCellReusableIdentify];    if (!cell) {        static NSInteger tag = 1;        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:sCellReusableIdentify];        cell.tag = tag++;    }    NSLog(@"tag:%ld", cell.tag);    return cell;}#pragma mark - UITableViewDelegate- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    return (SCREEN_HEIGHT) / 10;}

運行結果如下:

我們發現,無論我們怎麼滑動螢幕,cell的tag最大值也是11,那麼第1個問題的答案也就浮出水面了。第11個cell一定也是新申請的。

第2個問題,section的title也需要重用的(在iOS6以後),代碼如下:

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    _dataSource = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11"];    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];    _tableView.delegate = self;    _tableView.dataSource = self;    [_tableView setSeparatorColor:[UIColor redColor]];    [self.view addSubview:_tableView];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}#pragma mark - UITableViewDataSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return [_dataSource count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString *sCellReusableIdentify = @"sCellReusableIdentify";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sCellReusableIdentify];    if (!cell) {        static NSInteger tag = 1;        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:sCellReusableIdentify];        cell.tag = tag++;    }    NSLog(@"tag:%ld", cell.tag);    return cell;}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {    const int numberOfSections = 10;    return numberOfSections;}- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {    static NSString *sHeaderReusableIdentify = @"sHeaderReusableIdentify";    UITableViewHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:sHeaderReusableIdentify];    if (!view) {        view = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:sHeaderReusableIdentify];    }    return view;}#pragma mark - UITableViewDelegate- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    return (SCREEN_HEIGHT) / 10;}
3、寫一個函數或者宏實現將RGB轉化為一個UIColor *值.

同樣是考察c語言的基本功:

#define COLOR_WITH_RGB(rgb) [UIColor colorWithRed:((float)((rgb & 0xFF0000) >> 16)) \    green:((float)((rgb & 0xFF00) >> 8)) \    blue:((float)((rgb & 0xFF))) \    alpha:1]

有問題,歡迎批評指正!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.