ios-面试

来源:互联网 发布:evdo是什么网络几g 编辑:程序博客网 时间:2024/05/29 04:49

ios开发使用extern访问全局变量:

    1 、新建Constants.h文件(文件名根据需要自己取),用于存放全局变量;

        2、 在Constants.h中写入你需要的全局变量名,但是在定义时不能对其进行初始化,否则就出错例如:

                  NSString *url;//指针类型
                 int count;//非指针类型

        3  、在需要用到全局变量的文件中引入此文件:

                   #import "Constants.h"  

        4、给全局变量初始化或者赋值:

  1.  extern NSString *url;     //这里使用到extern c关键字,表示这个变量已经声明,只是引用。
  2.   
  3. url = [[NSString alloc] initWithFormat:@"http://www.hbcfg.net"];//指针类型;需要alloc   
  4.   
  5. extern int count;   
  6.   
  7. count = 0;//非指针类型  

       在使用全局变量的时候就和使用普通变量一样使用了。

当然还有其他的方法:可以在AppDelegate中声明并初始化全局变量,使用单例访问全局变量等


对于static修饰的变量

#import "SecondViewController.h"

static int count;

@implementation SecondViewController;

.......

-(void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

     NSLog(@"viewWillAppear is %d",count);

    count+=1;

}

这样你每进入一次界面就会发现count加1

除非程序完全退出重启,count才会清除,objective-c中用static修饰的变量和java中的静态变量是有区别的,其不能通过类名直接访问,当然你想直接访问也是能实现的在.m中写一个类方法反回count就行了,而且其作用域是其本类,不能扩展到其他类中


1.     Give usexample of what are delegate methods and whatare data source methods of uitableview.

1>   代理方法:返回tableView每行的高度、监听tableView每行的选中

2>   数据源方法:返回tableView数据的组数和行数、每行显示什么数据


2.       What are commonly used NSObject class methods?

NSObject常见的类方法有:alloc、new、description(类方法返回类名,对象方法返回类名+内存地址,nslog需要获取更多信息需要重写该方法)等




0 0
原创粉丝点击