小编Jos*_*ell的帖子

- [NSString writeToFile:]不会更改文件的内容

我正在尝试写入我的资源中的"index.html"文件.我可以毫无问题地加载文件,但我似乎无法写入它.没有任何东西显示为错误,它只是不写.该应用程序不会中止或任何东西,但当我重新加载文件时没有任何改变.

我的写作代码:

NSBundle *thisBundle = [NSBundle bundleForClass:[self class]];
NSString *path = [thisBundle pathForResource:@"index" ofType:@"html"];
NSString *myString = [[NSString alloc] initWithFormat:@""];
[myString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];
Run Code Online (Sandbox Code Playgroud)

我的加载代码:

[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

file-io cocoa-touch objective-c ios

2
推荐指数
1
解决办法
6200
查看次数

Objective-C方法如何访问被调用者的ivars?

我正在阅读Apple的文档,Objective-C Programming Language(PDF链接).在pg.18,在接收者的实例变量下,我看到了这一点.

方法可以自动访问接收对象的实例变量.您不需要将它们作为参数传递给方法.例如,primaryColor上面说明的方法不带参数,但它可以找到原色otherRect并返回它.每个方法都假定接收器及其实例变量,而不必将它们声明为参数.

此约定简化了Objective-C源代码.它还支持面向对象的程序员思考对象和消息的方式.信件被发送到接收器,就像信件被送到您的家一样.消息参数将信息从外部传递给接收者; 他们不需要将接收器带到自己身上.

我想更好地理解他们所描述的内容; 这是Python的self参数,还是风格?

oop objective-c instance-variables self

2
推荐指数
1
解决办法
208
查看次数

使用描述打印结构

我想知道是否可以使用descriptionCocoa框架中的函数来记录a的内容struct.例如:

    typedef struct {float a,b,c;}list;
    list testlist = {1.0,2.5,3.9};
    NSLog(@"%@",testlist); //--> 1.0,2.5,3.9
Run Code Online (Sandbox Code Playgroud)

cocoa struct objective-c nslog

2
推荐指数
1
解决办法
2945
查看次数

定义为返回id的SEL如何表示返回double的方法?

在Objective-C中有一个名为的类型SEL.它的定义如下:

id (*SEL)(id self, SEL _cmd,...);
Run Code Online (Sandbox Code Playgroud)

但是如果我们创建一个返回类型为的方法,则double该id类型不适合使用.

Objective-C运行时如何解决这个问题?

types objective-c return-type selector

2
推荐指数
1
解决办法
1286
查看次数

类方法和"在线分配的对象的潜在泄漏......"

我有这种情况:

- (void) foo {
    NSLog(@"Print this: %@", [MyObject classString]);
}

// So in MyObject.m I do
@implementation MyObject

+ (NSString *) classString {
     return [OtherObject otherClassString];   //The Warning "Potential leak..." is for this line
}
@end

// Finally in OtherObject
@implementation OtherObject

+ (NSString *) otherClassString {
    NSString *result = [[NSString alloc] initWithString:@"Hello World"];
    return result;
}
@end
Run Code Online (Sandbox Code Playgroud)

一开始,我对这项工作有一个警告otherClassString,classString但是otherClassString这样做.

现在我的问题是classString在MyObject.我尝试了很多东西,但这个警告总是显示出来.我不能在类方法中调用类方法吗?

memory-leaks class objective-c class-method

2
推荐指数
1
解决办法
529
查看次数

使用ARC时如何将UIButton保留在内存中?

我要做的是为每个UIButton创建的函数设置一个函数,并使用函数内部的开关来确定其行为.我在按钮上设置标签以使用开关,但它实际上并没有那么远.

for var(int i = 0; i < numResults; i++)
{
   UIButton* button = [[UIButton] alloc] initWithFrame:CGRectMake(0,(i*55)+10,320,50)];
   [buttton setTag:i];
   [button addTarget:self action:@selector(buttonHandler) forControlEvents:UIControlEventTouchUpInside];
   [self.view addSubview:button];
}

   ...

-(void) buttonHandler:(id)sender
{
    //Handle the button press
}
Run Code Online (Sandbox Code Playgroud)

当我点击任何按钮时,应用程序会抛出一个错误:

- [WatchViewController buttonHandler]:无法识别的选择器发送到实例0x6845430

我相信这是因为创建了按钮变量,然后由于没有对它的引用,由ARC自动释放,因此在调用该函数时它不再存在.不幸的是,我不知道如何保持对内存中每个按钮的引用.

如果我错了(或者我写的任何东西都是不好的做法),请随时告诉我 - 它会帮助我学习!

memory-management objective-c ios ios5 automatic-ref-counting

2
推荐指数
1
解决办法
349
查看次数

FoundationTool中的Runloop

我正在写一个基础工具.我必须将线程分成不同的正在进行的任务.

我试图做线程,但它一直在崩溃.最后我想出了我需要运行自己的runloop的原因.

有人可以用一些简单的例子来帮助吗?我试过跟随代码,但它不起作用.我每次运行它都会遇到异常崩溃?如何在Foundation工具中运行线程?

@interface MyClass : NSObject
{
}
-(void) doSomething;
@end

@implementation MyClass
-(void) RunProcess:
{
    printf("test");  
}
-(void) doSomething:
{
    [NSThread detachNewThreadSelector: @selector(RunProcess:) toTarget:self withObject: nil];  
}
@end

int main(void)
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    MyClass *myObj = [[MyClass alloc] init],
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1 myObj selector:@selector(doSomething:) userInfo:nil repeats:NO];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] run];

    [pool drain];
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

macos cocoa objective-c appkit runloop

2
推荐指数
1
解决办法
270
查看次数

设置遵循椭圆路径的CAKeyframeAnimation的起始位置

我试图UIView使用CAKeyframeAnimation跟随创建的路径的圆形动画CGPathAddEllipseInRect,我已经设置好了.但是,有没有办法设置动画的开始位置?我的代码是:

//set up animation
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration = 10.0;
pathAnimation.repeatCount = 1000;
CGMutablePathRef curvedPath = CGPathCreateMutable();

//path as a circle
CGRect bounds = CGRectMake(60,170,200,200);
CGPathAddEllipseInRect(curvedPath, NULL, bounds);

//tell animation to use this path
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);

//add subview
circleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ball.png"]];    
[testView addSubview:circleView];

//animate
[circleView.layer addAnimation:pathAnimation forKey:@"moveTheSquare"];
Run Code Online (Sandbox Code Playgroud)

cocoa-touch core-animation objective-c uiview cakeyframeanimation

2
推荐指数
1
解决办法
1332
查看次数

是否可以为每组连续的整数赋予名称?

我有一个int电话setupStage.这只是我在每个阶段完成时递增的值,所以我可以说if (setupStage == 2),而且我知道我处于第三阶段(默认为0).

有没有办法可以更好地参考这些数字?例如:

if (setupStage == stageEnterName)

而不必参考其实际原始int值.

这可能是一种误解,但我想要实现的是否与定义宏有关?

int objective-c naming-conventions

2
推荐指数
1
解决办法
62
查看次数

保护关键代码不被再次调用

我需要保护我的代码的关键区域,这是一个多线程.我希望防止在其他线程完成之前多次调用它.这就是我正在使用的:

- (void) filterAllEventsIntoDictionary{

    // start critical area
    if (self.sortedKeys.count != 0) {
        [self.sortedKeys removeAllObjects];
    }
    dispatch_async(self.filterMainQueue, ^{

        [self internal_filterAllEventsIntoDictionary]; 

        dispatch_sync(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

由于该internal_filterAllEventsIntoDictionary方法也可以访问self.sortedKeys,如果此代码被调用两次,它会因为removeAllObjects在开始时崩溃.

我仍然需要internal...在另一个线程中调用该方法,因为我不想阻止UI.那么,在dispatch_async呼叫仍未完成的情况下阻止此方法启动的最佳方法是什么?

cocoa locking objective-c blocking grand-central-dispatch

2
推荐指数
1
解决办法
291
查看次数