CGPoint and CGRect are structures (versus objects) and therefore the old NSLog standby %@ will not work as expected.

Here is how each structure is defined:

struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
 
struct CGRect {
CGPoint origin;
CGSize size;
};
typedef struct CGRect CGRect;

If you attempt NSLog to use the traditional ‘print object’ notation such as this:

// Print point structure using NSLog
CGPoint cgPoint = CGPointMake(1, 11);
NSLog(@"%@", cgPoint);

the compiler will generate a warning: Format specifies type ‘id’ but the argument has type ‘CGPoint’ (aka ‘struct CGPoint’) 

Good news is, this is easy to fix:

NSLog(@"Point: %@", NSStringFromCGPoint(cgPoint));

The output of converting a CGPoint to an NSString looks as follows:

Point: {1, 11}

Output CGRect using NSLog

Likewise, CGRect is a structure and will also give NSLog troubles. The solution is similar:

// Print rect structure using NSLog
CGRect rect = CGRectMake(5, 5, 10, 10);
NSLog(@"Rect: %@", NSStringFromCGRect(rect));
Rect: {{5, 5}, {10, 10}}

CGRect, CFDictionaryRef and NSLog

When working with CGRect structures, there is another solution, you can convert the rect to a CFDictionaryRef and print as shown below:

NSLog(@"CFDictionaryRef: %@", CGRectCreateDictionaryRepresentation(rect));
CFDictionaryRef: {
Height = 10;
Width = 10;
X = 5;
Y = 5;
}

If for some reason you need to keep the dictionary object around, here is how you can so if you are using ARC:

1
2
3
4
5
# Create reference to immutable CFDictionary
CFDictionaryRef currentListingRef = CGRectCreateDictionaryRepresentation(rect);
 
# Create Dictionary object managed by ARC
NSDictionary *dict = CFBridgingRelease(currentListingRef);

The code on line 5 moves the CGRef to and Objective-C object, and also hands the memory management over to ARC.

How to Use NSLog to Debug CGRect and CGPoint的更多相关文章

  1. 关于OC中直接打印结构体,点(CGRect,CGSize,CGPoint,UIOffset)等数据类型

    关于OC直接打印结构体,点(CGRect,CGSize,CGPoint,UIOffset)等数据类型,我们完全可以把其转换为OC对象来进项打印调试,而不必对结构体中的成员变量进行打印.就好比我们可以使 ...

  2. iOS 保存CGRect,CGPoint到NSArray'的方法

    由于CGRect和CGPoint等对象是Struct,即结构体,不是继承于NSObject的,所以需要先用NSValue的方法,把他们转化成NSValue对象,之后就可以存入NSArray了! @in ...

  3. ios开发之--CGRect/CGSize/CGPoint/CGVector/CGAffineTransform/UIEdgeInsets/UIOffset和NSString之间的转换

    仅做记录,一个函数和字符串之间的互相转换 方法如下: UIKIT_EXTERN NSString *NSStringFromCGPoint(CGPoint point); UIKIT_EXTERN N ...

  4. ios CGRect

    /*     rect(x,y,width,height);     width, height正负代表了从原点的绘制方向,矩形的长宽都是取得绝对值     */            // Do a ...

  5. ios NSLog常见使用

    NSLog常见输出格式 Table 1  Format specifiers supported by the NSString formatting methods and CFString for ...

  6. CGPoint、CGSize、CGRect and UIView

    首先要弄懂几个基本的概念. 一)三个结构体:CGPoint.CGSize.CGRect 1. CGPoint /* Points. */ struct CGPoint { CGFloat x; CGF ...

  7. iOS开发之 在release版本禁止输出NSLog内容

    因为NSLog的输出还是比较消耗系统资源的,而且输出的数据也可能会暴露出App里的保密数据,所以发布正式版时需要把这些输出全部屏蔽掉. 我们可以在发布版本前先把所有NSLog语句注释掉,等以后要调试时 ...

  8. UI基础:UIView(window,frame,UIColor,CGPoint,alpha,CGRect等) 分类: iOS学习-UI 2015-06-30 20:01 119人阅读 评论(0) 收藏

    UIView 视图类,视图都是UIView或者UIView子类 UIWindow 窗口类,用于展示视图,视图一定要添加window才能显示 注意:一般来说,一个应用只有一个window 创建一个UIW ...

  9. Xcode/iOS: 如何判断代码运行在DEBUG还是RELEASE模式下?

    原帖链接:http://stackoverflow.com/a/9063469 首先确定下项目的 Build Settings 是否已经设置过宏定义 DEBUG,如何看呢? 点击 Build Sett ...

随机推荐

  1. SecureCRT学习之道:用SecureCRT来上传和下载数据

    今天才知道,原来SecureCRT可以使用linux下的zmodem协议来快速的传送文件,而且还使用非常方便哦,我还傻傻的找其他软件来sftp,笨死了:(你只要设置一下上传和下载的默认目录就行opti ...

  2. POJThe Doors AND NYIST 有趣的问题

    POJThe Doors AND NYIST 有趣的问题 题目链接:pid=227" target="_blank">Click Here~ 题目分析: 给你横纵坐 ...

  3. BIEE11g BI_server Jvm參数调整

    1.找到user_projects\domains\bifoundation_domain\bin文件夹 2.复制startWeblogic.sh为新的文件startAdminWeblogic.sh, ...

  4. cocos2d 创建一个黑白纹理

    @interface myGrayTexture : CCTexture2D // @param exposure 曝光 +(id) textureWithFile:(NSString*) file ...

  5. WebForm / MVC 源码分析

    ASP.NET WebForm / MVC 源码分析   浏览器 Url:https//localhost:6565/Home/Index ,https//localhost:6565/WebForm ...

  6. 安装zookeeper集群

    zookeeper集群的安装   顾名思义zookeeper就是动物园管理员,他是用来管hadoop(大象).Hive(蜜蜂).pig(小猪)的管理员, Apache Hbase和 Apache So ...

  7. 小米2S Mk6.0.1 [只能做测试体验,不能使用]

    上几张高清图片.. 说明: 此版本只能做测试体验,不能做实际使用. 开发者: laser杨万荣 感谢: 秋叶随风ivan, m1cha 及 MoKee Open Source的各位开发者 下载地址:链 ...

  8. hadoop 提高hdfs删文件效率----hadoop删除文件流程解析

    前言 这段时间在用hdfs,由于要处理的文件比较多,要及时产出旧文件,但是发现hdfs的blocks数一直在上涨,经分析是hdfs写入的速度较快,而block回收较慢,所以分心了一下hadoop删文件 ...

  9. iOSSingleton设计模式详细的说明教程

    iOS有很多的设计模式,当然,不管是什么语言有很多的设计模式.辛格尔顿是一种之一,辛格尔顿,它从字面上是一个单独的实例,首先,它是只有一个单一的,其次,它是一个实例.我们知道,在iOS用于开发Obje ...

  10. java序列化是什么和反序列化和hadoop序列化

    1.什么是序列化和系列化DE- 神马是序列化它,序列化是内存中的对象状态信息,兑换字节序列以便于存储(持久化)和网络传输.(网络传输和硬盘持久化,你没有一定的手段来进行辨别这些字节序列是什么东西,有什 ...