在连接一个大字符串时,我得到一个EXEC_BAD_ACCESS.
我从一个feed读取并创建我的webview我建立了我的字符串,如:
NSString *pageData = @"<h1>header</h1>";
pageData = [pageData stringByAppendingFormat@"<p>"];
pageData = [pageData stringByAppendingFormat@"self.bodyText"];
pageData = [pageData stringByAppendingFormat@"</p>"];
etc
Run Code Online (Sandbox Code Playgroud)
我得到的问题是self.bodytext是21,089个字符,当我对字进行计数时有空格.这样做有更好的方法吗?
谢谢
所以我在一个方法中调用它:
-(id)initWithContentURL:(NSString *)url {
if (self = [super init]) {
NSLog(@"xSheetMusicViewController - %@",url);
// Casting an NSString object pointer to a CFStringRef:
CFStringRef cfString = (CFStringRef)url;
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), cfString, NULL, NULL);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
哪一个在NSLog的标记处发生了崩溃:
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), cfString, NULL, NULL);
Run Code Online (Sandbox Code Playgroud)
我从未见过这个奇妙的小错误.这是崩溃日志:
SheetMuse[83550:b603] -[NSURL _fastCharacterContents]: unrecognized selector sent to instance 0x4ec35f0
2011-09-22 17:36:22.921 SheetMuse[83550:b603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL _fastCharacterContents]: unrecognized selector sent to instance 0x4ec35f0'
*** Call …Run Code Online (Sandbox Code Playgroud) 我有一个函数,我想在NSString和一个int参数,然后使用switch语句,以返回一个计算值,如int乘以一些常量,具体取决于提供的NSString.
显然,switch语句不适用于Objective-C中的对象.那么最快的选择是什么?是if-else语句吗?还是有更优雅的方法?
编辑
我关心性能的原因是我正在修改用户正在观看的UI元素,这是这些计算的最终结果,我不希望它感到迟钝.
为什么这种比较会导致NO?
BOOL areTheSame = NSOrderedSame == [@"th" compare:@"They" options:NSCaseInsensitiveSearch range:NSMakeRange(0, 2)];
Run Code Online (Sandbox Code Playgroud)
当我测试它时@"th",@"Th"它就是YES.
我在这里错过了什么?
我有NSString这样的
NSString *string = @"textTextTextTextText<br>textTextTextText<br>TextTextText"
我想将此设置NSString为我的文本,在字符串UICell上
找到的每个标记上都有一个新行.我怎么能这样做?
我试过这个,没有成功:
cell.textLabel.text = [[text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@"<br>"];
NSString *str = @" My name is Mike, I live in California and I work in Texas. Weather in California is nice but in Texas is too hot...";
Run Code Online (Sandbox Code Playgroud)
我怎样才能遍历这个NSString并为每次出现的"California"获取NSRange,我想要NSRange,因为我想在NSAttributed字符串中更改它的颜色.
NSRange range = NSMakeRange(0, _stringLength);
while(range.location != NSNotFound)
{
range = [[attString string] rangeOfString: @"California" options:0 range:range];
if(range.location != NSNotFound)
{
range = NSMakeRange(range.location + range.length, _stringLength - (range.location + range.length));
[attString addAttribute:NSForegroundColorAttributeName value:_green range:range];
}
}
Run Code Online (Sandbox Code Playgroud) 我怎样才能从获得所选文本的字符串NSTextView作为NSString?
非常感谢您的帮助.
我目前正在尝试通过阅读书籍和在线教程来学习Objective-C,同时参考Apple文档,但有些事情只是不要点击.我有关于类的问题,我已经使用NSString一段时间了,而没有过多地关注它的使用方式.
我的印象是,为了让某人能够使用Objective-C中某个类的方法,你首先需要实例化它,比如...
ClasssName *varName = [[ClassName alloc]init];
Run Code Online (Sandbox Code Playgroud)
然后你会调用像......这样的方法
[varName someMethod];
Run Code Online (Sandbox Code Playgroud)
但是看看如何使用NSString我现在有点困惑,例如这里我们通常会如何使用它...
NSString *someString = @"some text here";
[someString stringByAppendingFormat: @"some text = %d", 3];
Run Code Online (Sandbox Code Playgroud)
按照我所读到的关于类的内容,我们需要做以下的事情.
NSString *someString = [[NSString alloc]initWithString: @"some text here"];
[someString stringByAppendingFormat: @"some text = %d", 3];
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么有些类在使用其方法之前不需要实例化吗?
我正在使用以下两种方法(一种是类别,NSString另一种是类别UILabel),根据标签内的文本自动调整标签的高度.它适用于大部分,但它有一些不可预测的结果.我不太确定问题可能发生在哪里,我希望你们中的一些人可以提供帮助.首先,这里是有问题的方法:
NSString类别:
- (CGFloat) fontSizeWithFont: (UIFont *) font constrainedToSize: (CGSize) size minimumFontSize: (CGFloat) minimumFontSize
{
CGFloat fontSize = [font pointSize];
CGFloat height = [self sizeWithFont: font constrainedToSize: CGSizeMake(size.width, FLT_MAX) lineBreakMode: NSLineBreakByWordWrapping].height;
UIFont *newFont = font;
// Reduce font size while too large, break if no height (empty string)
while (height > size.height && height != 0 && fontSize > minimumFontSize) {
fontSize--;
newFont = [UIFont fontWithName: font.fontName size: fontSize];
height = [self sizeWithFont: newFont constrainedToSize: CGSizeMake(size.width, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用投影来绘制多行文本,而不使用已弃用的API.它适用于单行.相关代码如下所示:
-(void)drawRect:(CGRect)rect
{
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
paragraph.lineBreakMode = NSLineBreakByWordWrapping;
paragraph.alignment = NSTextAlignmentCenter;
UIFont *f = [UIFont systemFontOfSize:20.0];
NSMutableDictionary *attributes = [NSMutableDictionary new];
[attributes setValuesForKeysWithDictionary:@{ NSFontAttributeName : f,
NSParagraphStyleAttributeName : paragraph,
NSForegroundColorAttributeName : [UIColor blueColor] }];
NSShadow * shadow = [NSShadow new];
shadow.shadowOffset = CGSizeMake(4,4);
shadow.shadowColor = [UIColor redColor];
[attributes setValue:shadow forKey:NSShadowAttributeName];
rect.origin.y = 100;
[@"test string on one line" drawInRect:rect withAttributes:attributes];
rect.origin.y = 150;
[@"test string spanning more than one line" drawInRect:rect withAttributes:attributes];
}
Run Code Online (Sandbox Code Playgroud)
输出看起来像这样:

我在iPhone 5(7.1.2),iPhone …
nsstring ×10
objective-c ×6
ios ×5
xcode ×2
cfstring ×1
cocoa ×1
cocoa-touch ×1
init ×1
ios8 ×1
ipad ×1
line-breaks ×1
macos ×1
nsrange ×1
nsshadow ×1
nstextview ×1
performance ×1
uilabel ×1
uitextview ×1