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. Troubleshooting "Global Enqueue Services Deadlock detected" (Doc ID 1443482.1)

    In this Document   _afrLoop=1021148011984950&id=1443482.1&displayIndex=1&_afrWindowMode= ...

  2. 利用sendmsg和recvmsg来指定发送接口或者获取接收数据接口

    前言     sendmsg和recvmsg函数是一对相对下层的套接字发送.接受函数. 通过这对函数,我们能够设置或者取得数据包的一些额外的控制信息.这些信息中比較经常使用的就是本文要介绍的发送.接受 ...

  3. An Overview of Complex Event Processing

    An Overview of Complex Event Processing 复杂事件处理技术概览(一) 翻译前言:我在理解复杂事件处理(CEP)方面一直有这样的困惑--为什么这种计算模式是有效的, ...

  4. [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 D ...

  5. 关于读style元素定义样式表兼容性

    <span style="font-size:18px;"></span><pre name="code" class=" ...

  6. Asp.net网站的简单发布

    概述 网站是由一个个页面组成的,是万维网具体的变现形式,关于万维网,网页的方面的理论知识,大家可以看一看这篇博客:万维网文档,在这里就不多说了.网站的发布要到达的一个目的就是,别人可以通过浏览器访问该 ...

  7. HDOJ 2665 Kth number

    静态区间第K小....划分树裸题 Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  8. opencv环境的搭建,并打开一个本地PC摄像头。

    1.opencv环境结构 推荐连结 http://www.cnblogs.com/Anykong/archive/2011/04/06/Anykong_OpenCV1.html 2.以下是基本測试,和 ...

  9. DiskFileUpload类

    1.2.2 DiskFileUpload类 DiskFileUpload类是Apache文件上传组件的核心类,应用程序开发者通过这个类来与Apache文件上传组件进行交互.以下介绍DiskFileUp ...

  10. 阅读&lt;反欺骗的艺术&gt;思考

    早期接受csdn发送本书<反欺骗的艺术 ---- 传说中的黑客世界里,分享经验>. 经过这本书发生床头, 每天晚上看上.直到今天, 刚读的书. 颇有感觉. 之所以当初选择读这本书, 完全被 ...