ios开发-OC的初步学习


  今天开始我们正式学习OC了,OC是在C语言基础上进行封装的,是一门面向对象的语言,而之前学的C语言是面向过程语言,今天初步学习下OC的基本知识:

1.coc的区别:

    oc属于面向对象:面向过程关注的是解决问题需要哪些步骤

    c属于面向过程:面向过程关注的是解决问题需要哪些步骤

2.类和对象的关系:

    类和对象是面向对象中两个非常重要的概念:

    类指的是具有一定属性的元素的集合,对象是直接用来操作的操作者.

3.oc中用到类的时候用如下步骤:

 

  1.声明文件

 #import <Foundation/Foundation.h>

 // 类的声明

 @interface Car : NSObject//这个使得Car类型具有创建对象的能力

 {    //大括号,初始化成员变量为0!

     @public//可以让Car对象的成员变量/实例变量属性被外界访问

     int wheels; // 多少个轮子

     int speed; // 时速

 }

 - (void)run; // 跑的行为,类型一定有小括号(定义的方法类型)

 @end

 

 2.类的实现

 @implementation Car

 - (void) run

 {

    NSLog(@"%i个轮子,%i时速的车子跑起来了", wheels, speed);

 }

 @end

 

3.创建对象

 // 主函数

 int main()

 {

     // 创建车子对象

     Car *c = [Car new];//定义一个Car类型的指针c,指向[Car new]地址

     c->wheels = 3;//进行赋值

     c->speed = 300;

     

     [c run];//

     return 0;

 }

创建类的练习题:

 #import <Foundation/Foundation.h>

 //typedef-枚举性别

 typedef enum {

 Sexman,

 Sexwoman,

 }Sex;

 //typedef-年龄结构体

 typedef  struct {

 int year;

 int month;

 int day;

 }Date;

 //typedef-枚举颜色

 typedef enum {

 ColorBlack,

 ColorRed,

 ColorGreen,

 }Clor;

 //声明,构造类-Dog

 @interface Dog : NSObject

 {

 @public

 double weight;

 Clor curcolor;

 }

 - (void)eat;//声明方法

 - (void)run;

 

 @end

 //Dog-方法的实现

 @implementation Dog

 - (void)eat

 {

 weight += 1;

 NSLog(@"吃完这次的体重是%f", weight);

 }

 - (void)run

 {

 weight -=1;

 NSLog(@"跑完这次的体重是%f", weight);

 

 }

 @end

 

 

 

 //声明,构造结构体-Student

 @interface Student : NSObject//允许Student可以创造对象

 {

 @public//允许外部指针改变变量

 Sex sex;

 Date birthday;

 double weight;

 Clor favColor;

 char *name;

 //重点:,构造一个Dog类型的变量,使得每个人都有一个dog

 Dog *dog;

 }

 - (void)eat;//方法的声明

 - (void)run;

 - (void)printf;

 @end

 //Student-方法的实现

 @implementation Student

 //方法eat

 - (void)eat

 {

 weight += 1;

 NSLog(@"吃完这次的体重是%f", weight);

 }

 //方法run

 - (void)run

 {

 weight -=1;

 NSLog(@"跑完这次的体重是%f", weight);

 

 }

 //方法myprint

 - (void)printf

 {

 NSLog(@"性别是%d,喜欢的颜色是%d,体重是%f,姓名是%s,生日是%d-%d-%d", sex, favColor, weight, name, birthday.year, birthday.month, birthday.day);

 

 }

 

 @end

 //main函数主体

 int main(int argc, const char * argv[]) {

 @autoreleasepool {

 //构造一个Student类型的指针s,指向[Student new]

 Student *s =[Student new];

 //构造一个Dog类型的狗

 Dog *d = [Dog new];

 d->curcolor =  ColorGreen;

 d->weight = 50;

 s->dog =d;

 s->weight =50;

 //性别

 s->sex = Sexman;

 //生日

 

 s->birthday.year = 2012;

 s->birthday.month = 12;

 s->birthday.day = 29;

 

 //喜欢的颜色

 s->favColor = ColorBlack;

 //名字

 s->name = "jack";

 [s printf];

 

 }

 return 0;

 }

总结:

要注意一下几点:

1)充分理解面向对象的含义;

2)注意OC中是如何定义类的,以及类的声明,实现等.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS开发中上传图片可以采用以下步骤: 1.选择要上传的图片,可以使用系统提供的UIImagePickerController控制器,或者使用第三方库,例如TZImagePickerController。 2.将选中的图片转换为NSData格式。 3.使用NSURLSession或AFNetworking等网络库,将图片数据上传到服务器。 以下是一个简单的上传图片的示例代码: ``` // 选择图片 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker.delegate = self; [self presentViewController:imagePicker animated:YES completion:nil]; // 将选中的图片转换为NSData格式 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info { UIImage *selectedImage = info[UIImagePickerControllerOriginalImage]; NSData *imageData = UIImageJPEGRepresentation(selectedImage, 0.5); // 上传图片到服务器 NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; NSURL *url = [NSURL URLWithString:@"http://example.com/upload.php"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"POST"; NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:imageData completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { // 处理服务器返回的响应 }]; [uploadTask resume]; [picker dismissViewControllerAnimated:YES completion:nil]; } ``` 其中,upload.php是服务器端接收图片的脚本文件。在服务器端,可以使用PHP等语言来处理上传的图片数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值