关于处理无效*的帖子很多.我已按照他们的指示行事.但是,当尝试将void*转换为NSString*时,我收到运行时错误.
我很确定我错过了一些简单的东西.:-)
请考虑以下代码:
NSString* testString = @"This is a test";
NSLog(@"voidTest = %@", [self voidTest:&testString]);
Run Code Online (Sandbox Code Playgroud)
并具有以下功能:
- (NSString*)voidTest:(void*)testString
{
NSString* value = (__bridge NSString *)(testString);
return value;
}
Run Code Online (Sandbox Code Playgroud)
运行此代码时,函数中的第一行会给出以下错误:
Thread 1: EXC_BAD_ACCESS(code=1, address=0x1200000070)
Run Code Online (Sandbox Code Playgroud)
任何有关这方面的帮助将不胜感激.
我有一个来自服务器API的NSDictionary对象.数据是这样的,
"filesize_mp3": "60488"
当我使用以下代码来检索文件大小时,应用程序崩溃.obj.filesize是long类型.
obj.filesize = [[dict objectForKey:@"filesize_mp3"] longValue],
Run Code Online (Sandbox Code Playgroud)
lldb控制台,
2014-07-22 12:50:14.643 YouVoiceNews[2019:60b] -[__NSCFString longValue]: unrecognized selector sent to instance 0x9bf8e10 2014-07-22 12:50:14.644 YouVoiceNews[2019:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString longValue]: unrecognized selector sent to instance 0x9bf8e10'
但是当我用的时候
obj.filesize = (long)[dict objectForKey:@"filesize_mp3"];
Run Code Online (Sandbox Code Playgroud)
一切都好.
所以我的问题是上述两种方式之间的区别是什么.为什么第一个导致应用程序崩溃
提前致谢.
我正在努力获得基于可接受的可变大小文本的UILabel所需的高度.经过几个小时的研究,我还没有找到一种可行的方法来实现这一目标.目前我的代码如下:
func getHeightForTitle(postTitle: NSString) -> CGFloat {
// Get the height of the font
let constraintSize = CGSizeMake(self.cellTextWidth, CGFloat.max)
let attributes = [NSFontAttributeName: [UIFont.systemFontOfSize(16.0)]]
let labelSize = postTitle.boundingRectWithSize(constraintSize,
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: attributes,
context: nil)
return labelSize.height
}
Run Code Online (Sandbox Code Playgroud)
但是这会引发以下错误:
2014-08-02 12:09:37.370 Testing App[8365:351906] - [_TtCSs23_ContiguousArrayStorage00007FD26C15F708 pointSize]: unrecognized selector sent to instance 0x11e640050
Run Code Online (Sandbox Code Playgroud)
这总是在let labelSize = postTitle ...方法中抛出,我相信它归结为属性变量.但是我可能错了.任何帮助表示赞赏,非常感谢!
请注意:这适用于iOS 8 Swift开发项目.
我已经使用下面的代码将NSArray转换为NSString假设我有一个包含一些数据的数组.
NSString *sample=[array description];
NSLog(@"%@",sample);
Run Code Online (Sandbox Code Playgroud)
打印:
(
{
URL = "1a516af1a1c6020260a876231955e576202bbe03.jpg##37911944cc1ea8fd132ee9421a7b3af326afcc19.jpg";
userId = 0;
wallpaperId = 31;
},
{
URL = "a9356863fa43bc3439487198283321622f88e31f.jpg##f09c743ebdc26bb9f98655310a0529b65a472428.jpg";
userId = 0;
wallpaperId = 30;
}
)
Run Code Online (Sandbox Code Playgroud)
它看起来像数组,但它实际上是一个字符串.现在我想知道,我怎样才能重新回到NSArray?帮助赞赏.
请这不是一个重复的问题,我无法在任何地方找到相同的问题.
我有一个包含数字的字符串数组.所以,如果有得到的方法NSNumber,从NSString它会很容易得到与数字数组:
[arrayOfStrings valueForKey:@"NSNumberValue"]
Run Code Online (Sandbox Code Playgroud)
但要将字符串转换为数字,您必须执行以下操作:
[NSNUmber numberWithInt:[string intValue]];
Run Code Online (Sandbox Code Playgroud)
我想知道如果没有手动迭代数组有没有办法做到这一点?
我想检查字符串是否只包含字母数字.(仅限字母和数字).我试过用NSCharacterSet *strCharSet = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"];
我也试过这个NSString *string = @"[^a-zA-Z0-9]";=没有运气
但它只检查字符串是否包含任何字符或仅包含此字符.我需要用户输入字母和数字.
-update我有一个密码的文本字段.我需要用户输入字母和密码.但我无法检查文本字段是否包含两个字母和数字.通过使用上面的代码,它仅检查文本字段的文本是否仅包含字母数字.
我只是编写以下代码用于测试目的:
NSString *aStr = [[NSString alloc] initWithFormat:@"Foo"];
aStr = [aStr initWithFormat:@"Bar"];//Crashed here
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
*** initialization method -initWithFormat:locale:arguments: cannot be sent to an abstract object of class __NSCFString: Create a concrete instance!
Run Code Online (Sandbox Code Playgroud)
如果我写下面的代码同样的事情发生
NSString *aStr = [NSString alloc];
aStr = [aStr initWithFormat:@"Foo"];
aStr = [aStr initWithFormat:@"Bar"]; //Crashed here
Run Code Online (Sandbox Code Playgroud)
通过谷歌,我知道initWithFormat将返回NSCFString对象.我的问题是,如果NSCFString是派生类,NSString那么为什么我不能调用该initWithFormat方法NSCFString.如果可以停止可见性,我该如何在代码中实现.
我正在使用Objective C编写Xcode中的计算器进行学习(我是初学者.).这是我正在使用的代码(计算NSString).
- (IBAction)resultButtonPressed:(id)sender {
NSExpression *expression = [NSExpression expressionWithFormat:label_result.text];
label_result.text = [NSString stringWithFormat:@"%@", [expression expressionValueWithObject:nil context:nil]];
}
Run Code Online (Sandbox Code Playgroud)
问题是,只要用户键入"不可计算"的内容,如"6(5 + 1)"而不是"6*(5 + 1)",程序就会崩溃并且我得到这个:
2014-11-01 02:36:10.577 The Calculator[1515:575020] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "6(5+1) == 1"'
*** First throw call stack:
(0x18672de48 0x196e280e4 0x18753ad98 0x187538e2c 0x18764b128 0x18764b0c4 0x100062244 0x18af110f8 0x18aefa22c 0x18af10a94 0x18af10720 0x18af09c74 0x18aedd38c 0x18b17c1b4 0x18aedb8f4 0x1866e60e8 0x1866e538c 0x1866e343c 0x1866111f4 0x18f7a75a4 0x18af42784 0x10006286c 0x197496a08)
libc++abi.dylib: terminating with uncaught exception of type …Run Code Online (Sandbox Code Playgroud) 我的问题分为两部分:
NSString *bar = @"bar";
NSString *foobar = @<@"foo %@", bar>
我在整个项目中使用了stringWithFormat方法,我认为这对代码简单性和整洁性都非常有用.
当我使用硬编码数据的直径和高度Swift运行模拟器很好,但当我尝试使用TextField.text.toInt()使用文本值,然后我不断收到非常恼人的错误消息: - 无法调用\ $ st15类型的参数列表
我哪里错了.我是Swift的新手,之前只使用过AppInventor来创建一个有几千次下载的应用程序.我是一个热情但可能很慢的学习者,但如果有人能帮助我一点点,我会到那儿.注意:公式只是PIr2 xh来给出汽缸容积.我想用直径来解释我每次减半的原因.
let PI = 3.142
var bodyDiameter = bodyDiameterTextField.text.toInt() // 3.0
var bodyHeight = bodyHeightTextField.text.toInt() // 10.0
var cylinderVolume: Double
var cylinderVolume = (PI * (bodyDiameter / 2.0) * (bodyDiameter / 2.0)) * bodyHeight
println("cylinderVolume")
cylinderVolumeLabel.text = "(cylinderVolume)"
Run Code Online (Sandbox Code Playgroud)
以下是与Bodydiameter和Bodyheight的硬编码值相同的代码.这一切在游乐场和模拟器中都很棒.我猜它与Integers和Floats有关,但我可能会出局.
let PI = 3.142
var bodyDiameter = 3.0
var bodyHeight = 10.0
var cylinderVolume = (PI * (bodyDiameter / 2.0) * (bodyDiameter / 2.0)) * bodyHeight
println("cylinderVolume")
cylinderVolumeLabel.text = "(cylinderVolume)"
Run Code Online (Sandbox Code Playgroud) nsstring ×10
objective-c ×7
ios ×5
swift ×2
alphanumeric ×1
casting ×1
ios7 ×1
ios8 ×1
iphone ×1
macros ×1
nsarray ×1
nsexception ×1
nsnumber ×1
uilabel ×1