#pragma mark的用法

本文详细介绍了如何使用Xcode中的#pragmamark来组织和标记代码,通过在Function Menu中创建可视化的分隔符和标签,提高代码的可读性和维护性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(A) #pragma mark是Objective -C语言的一部份吗?
答:不是。它纯粹是Xcode的工具,对程式一点影响都没有。若不想深入了解,直接跳过即可。

(B) #pragma mark的功用是什麼?
答:pragma这字在字典上查不到,近似字pragmatic的意义是活跃的、爱管閒事的、实用主义的。pragma mark实质上是让Xcode在其Function Menu内打上一些记号,Function Menu就是图一中两个星号之下的那一块,Function Menu列出程式档内的class (左方有c字样)、function(左方有f字样)、method(左方有m字样),这是Xcode让使用者可以快速查看一个程式档案裡所有class、method、function的工具,pragma mark则是在Function Menu内作记号(mark),帮助阅读程式。

#pragma mark有两种型式,
一是 #prama mark - (如图一程式第3行)
在Function Menu会出现一条直线,如图一的两个星号之间,@interface HelloController之上的那一条细线。

当comment掉第3行后,Function Menu上的那一条细线就没有了(见图二)。

或是 #pragma mark UITableViewDataSource Methods (以 UITableViewDataSource Methods 為例 ) ((如图一程式第16行)

在Function Menu内会出现#pragma mark后的所有字(并且以boldface呈现),如图一的两个+号之间的" UITableViewDataSource Methods "。

当comment掉第16行后,Function Menu上的UITableViewDataSource Methods就没有了(见图二)。
可以看一下图二方块围住的Function Menu和图一有何差异。是否用#pragma mark的图一清楚很多,

有两条直线分隔HelloController和SampleAppDelegate两个class。
在HelloController class中,又分出init、 UITableViewDataSource Methods、 UITableViewDelegate Methods。

这就是#pragma mark的功能及用法 。

图一:


图二:






#pragma mark -- UIImagePickerControllerDelegate- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ UIImage *pickImage = info[UIImagePickerControllerOriginalImage]; CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy: CIDetectorAccuracyHigh}]; // 获取选择图片中识别结果 NSArray *features = [detector featuresInImage:[CIImage imageWithData:UIImagePNGRepresentation(pickImage)]]; [picker dismissViewControllerAnimated:YES completion:^{ if (features.count > 0) { CIQRCodeFeature *feature = features[0]; NSString *stringValue = feature.messageString; [self handleScanValue:stringValue]; } else { [self readFromAlbumFailed]; } }];}- (void)changeVideoScale:(AVMetadataMachineReadableCodeObject *)objc { NSArray *array = objc.corners; CGPoint point = CGPointZero; int index = 0; CFDictionaryRef dict = (__bridge CFDictionaryRef)(array[index++]); // 把点转换为不可变字典 // 把字典转换为点,存在point里,成功返回true 其他false CGPointMakeWithDictionaryRepresentation(dict, &point); NSLog(@"X:%f -- Y:%f",point.x,point.y); CGPoint point2 = CGPointZero; CGPointMakeWithDictionaryRepresentation((__bridge CFDictionaryRef)array[2], &point2); NSLog(@"X:%f -- Y:%f",point2.x,point2.y); NSLog(@"bounds:%@",NSStringFromCGRect(objc.bounds));}#pragma mark -- App 从后台进入前台- (void)appDidBecomeActive:(NSNotification *)notify { [self resumeScanning];}#pragma mark -- App 从前台进入后台- (void)appWillResignActive:(NSNotification *)notify { [self pauseScanning];}/** 恢复扫一扫功能 */- (void)resumeScanning { if (self.session) { [self.session startRunning]; [self.scannerView startLineAnimation]; }}/** 暂停扫一扫 */- (void)pauseScanning { if (self.session) { [self.session stopRunning]; [self.scannerView stopLineAnimation]; }}#pragma mark -- 扫一扫API/** 处理扫一扫结果 @param value 扫描结果 */- (void)handleScanValue:(NSString *)value { NSLog(@"handleScanValue === %@", value);}/** 相册选取图片无法读取数据 */- (void)readFromAlbumFailed { NSLog(@"readFromAlbumFailed");}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819
最新发布
04-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值