为什么以下代码在底部生成日志记录?这是异常 - 我的第二个NSLog应该打印chrStr但不产生任何空白,这由此debug命令验证:
(gdb)po chrStr
object返回空描述
但是,我将NSString重新转换回NSData对象的第三个NSString显示数据,与第一个NSLog中的值相同,应该如此.这将向我表明chrStr必须具有实际内容.但是从NSLOG或po命令看来似乎并非如此.为什么?
NSString *login;
NSString *pass;
// Purpose: NSString *loginString = [NSString stringWithFormat:@"\000%@\000%@", login, pass];
login = @"Loginname"; // text string1
pass = @"Password"; // text string2
// convert text strings to data objects
NSData *subData1 = [login dataUsingEncoding:NSUTF8StringEncoding];
NSData *subData2 = [pass dataUsingEncoding:NSUTF8StringEncoding];
// embed a NULL into new NSData object
NSMutableData *data = [NSMutableData data];
unsigned char zeroByte = 0;
[data appendBytes:&zeroByte length:1];
// append string1, NULL, string2 to data object
[data appendData:subData1]; …Run Code Online (Sandbox Code Playgroud) 我正在调度队列中的完成块上阅读以下Apple文档,我无法理解其中的一部分.该文件提到,"为了防止过早释放队列,关键是要保持最初该队列,并释放它一旦完成块已经发出了." 这与我的理解相矛盾,即块在块闭包中保留了所有变量,块编程指南中提到了这些变量.
我在这里错过了什么?文档的片段粘贴在下面:
完成块只是您在原始任务结束时分派到队列的另一段代码.调用代码通常在完成任务时将完成块作为参数提供.所有任务代码必须做的是在完成其工作时将指定的块或函数提交到指定的队列.
清单3-4显示了使用块实现的平均功能.平均功能的最后两个参数允许调用者指定报告结果时要使用的队列和块.在averaging函数计算其值后,它会将结果传递给指定的块并将其分派到队列中.为防止队列过早释放,最初保留该队列并在分派完成块后释放该队列至关重要.清单3-4在任务之后执行完成回调
void average_async(int *data, size_t len, dispatch_queue_t queue, void (^block)(int))
{
// Retain the queue provided by the user to make
// sure it does not disappear before the completion
// block can be called.
dispatch_retain(queue);
// Do the work on the default concurrent queue and then
// call the user-provided block with the results.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
int avg = average(data, len);
dispatch_async(queue, ^{ block(avg);});
// Release the user-provided queue when done
dispatch_release(queue);
});
}
Run Code Online (Sandbox Code Playgroud) 我正在阅读Bovet和Cesati的"理解Linux内核"一书.在第二章中,在"在Linux中进行分页"一书中,作者提到了如何使用32个没有启用PAE的体系结构来消除Page Middle和Upper Directories.我无法理解作者的意思.
他们的治疗方法已经松动,并没有很多直观的意义.
对于没有物理地址扩展的32位体系结构,两个分页级别就足够了.Linux本质上通过声明它们包含零位来消除Page Upper Directory和Page Middle Directory字段.但是,保留了页面上层目录和页面中间目录在指针序列中的位置,以便相同的代码可以在32位和64位体系结构上工作.内核通过将其中的条目数设置为1并将这两个条目映射到页面全局目录的正确条目,为页面上层目录和页面中间目录保留一个位置.
有人能以更可口的方式解释这一点吗?
所以我得到了以下操作:
NSInteger articleIndex = self.featuredArticlesCounter % self.featuredArticles.count;
Run Code Online (Sandbox Code Playgroud)
在这种情况下,self.featuredArticlesCounter为-1,
self.featuredArticles.count为10
所以它基本上是-1%10,应该是9,但结果是5.
谷歌称这是9.
如果我这样做NSInteger articleIndex = -1 % 10;会给我-1
我已经尝试将NSUInteger从count转换为NSInteger,但这不起作用.我试过在所有地方插入括号,但这也不起作用.
我已经切换到使用了((-1 % 10) + 10) % 10.
但我仍然想知道这笔交易是什么.有任何想法吗?
编辑:
featuredArticlesCounter是一个签名的int
self.featuredArticles.count是一个unsigned int
我正在汇编程序中编写一个程序并且它不起作用,所以我想在x86函数中输出变量以确保值是我期望的值.有一种简单的方法可以做到这一点,还是非常复杂?
如果它更简单,则从C函数中使用汇编函数,并使用gcc进行编译.
我看到一种奇怪的行为,我需要一些帮助.
在structure.h我有:
typedef struct {
NSString *summary;
NSArray *legs;
NSString *copyrights;
struct polylineSruct overview_polyline;
struct directionBounds bounds;
} route;
typedef struct {
NSArray *routes;
NSString *status;
} directions;
Run Code Online (Sandbox Code Playgroud)
在结构中,我有:
(directions) a_Function_that_builds_the_struct
{
directions direct;
direct.status = @"OK";
NSMutableArray *routes = [NSMutableArray array];
for(xxx)
{
route routeL;
routeL.summary = @"1";
routeL.copyrights = @"2";
NSValue *boxedroute = [NSValue valueWithBytes:&routeL objCType:@encode(route)];
[routes addObject:boxedroute];
}
direct.routes = routes;
return direct;
}
Run Code Online (Sandbox Code Playgroud)
在list_itemsViewController.h我有:
implementation XXX:YYY{
directions directionsLoc;
}
@property (assign) directions directionsLoc;
Run Code Online (Sandbox Code Playgroud)
在list_itemsViewController.h我有:
@synthesize directionsLoc; …Run Code Online (Sandbox Code Playgroud) 我可以#ifdef #imports在objective-c吗?
例如:
#ifdef USE_A
#import "ClassA.h"
#endif
#ifdef USE_B
#import "ClassB.h"
#endif
Run Code Online (Sandbox Code Playgroud) 如果我创建一个带回调的线程,比如..
NSAutoreleasePool* pool = [NSAutoreleasePool alloc] init];
while(1) {
//Process Stuff
}
[pool release];
Run Code Online (Sandbox Code Playgroud)
我认为任何自动释放的东西永远不会被释放,因为池永远不会被耗尽.我可以改变这样的事情:
while(1) {
NSAutoreleasePool* pool = [NSAutoreleasePool alloc] init];
//Process Stuff
[pool release];
}
Run Code Online (Sandbox Code Playgroud)
但是经常分配/删除似乎有点浪费.有没有办法可以留出一块内存,一旦它完全释放池?
Xcode告诉我,我错过了一个')',但我不知道它应该去哪里; 对我来说完全是无稽之谈.已注释掉错误消息.
你能看一下代码并告诉我哪里错了吗?
谢谢.
CalculatorViewController.m
#import "CalculatorViewController.h"
@interface CalculatorViewController()
@property (nonatomic) BOOL userIsInTheMiddleOfEnteringANumber;
@end
@implementation CalculatorViewController
@synthesize display;
@synthesize userIsInTheMiddleOfEnteringANumber;
- (IBAction)digitPressed:(UIButton *)sender
{
NSString *digit = [sender currentTitle];
self.display.text = [self.display.text stringByAppendingString:digit];
if (self userIsInTheMiddleOfEnteringANumber) { // Expected ')'
self.display.text = [self.display.text stringByAppendingString:digit];
} else {
self.display.text = digit;
self.userIsInTheMiddleOfEnteringANumber = YES;
}
}
@end
Run Code Online (Sandbox Code Playgroud)