我试图获得NSString的宽度(例如NSString*myString = @"hello").有没有办法做到这一点?
谢谢.
我已更新Xcode到7.3,现在我对用于创建随机字符串的函数发出警告.
我试图改变for声明, for (i in 0 ..< len){...}但是警告变成了错误.
如何删除警告?
static func randomStringWithLength (len : Int) -> NSString {
let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let randomString : NSMutableString = NSMutableString(capacity: len)
for (var i=0; i < len; i += 1){ // warning
let length = UInt32 (letters.length)
let rand = arc4random_uniform(length)
randomString.appendFormat("%C", letters.characterAtIndex(Int(rand)))
}
return randomString
}
Run Code Online (Sandbox Code Playgroud) 什么是自动生成的getter和setter看起来像以下属性值?
... in .h
@interface MyClass : NSObject {
@private
NSString *_value;
}
@property(retain) NSString *value;
... in .m
@synthesize value = _value;
Run Code Online (Sandbox Code Playgroud)
如果我改变属性怎么办?
@property(retain, readonly) NSString *value;
Run Code Online (Sandbox Code Playgroud)
特别是我对故事的原子部分感兴趣,加上保留,如果可能的话,详细的代码将更清楚地说明幕后发生了什么.
我已经看过很多Javascript解决方案来做到这一点,但我确信必须有一个更简单的方法.
我有一个非常简单的表单 - 一个多行文本框和一个提交按钮.我希望用户能够提交"格式化"文本(例如电子邮件,段落,新行等)
但是,当用户按Enter键放入回车符时,它会提交表单.
我在那里必须有一个属性或控制它的东西,因为这必须是一个常见的问题.Javascript作为解决方案似乎有点过于复杂.
在最近的一次采访中,我被要求破译这个正则表达式
^\^[^^]
Run Code Online (Sandbox Code Playgroud)
你能帮帮我吗?另外请提供一些链接,我可以在那里学习正则表达式的访谈.
新的python.这可能很简单,但我没有找到答案.
rndStr = "20101215"
rndStr2 = "20101216"
str = "Looking at dates between 20110316 and 20110317"
outstr = re.sub("(.+)([0-9]{8})(.+)([0-9]{8})",r'\1'+rndStr+r'\2'+rndStr2,str)
Run Code Online (Sandbox Code Playgroud)
我正在寻找的输出是:
Looking at dates between 20101215 and 20101216
Run Code Online (Sandbox Code Playgroud)
但相反,我得到:
P101215101216
Run Code Online (Sandbox Code Playgroud)
两个rndStr的值并不重要.假设它是随机的或取自用户输入(我在这里放置静态val以保持简单).谢谢你的帮助.
在swift2.3 ++中,运算符用于string.index的增加
例如.我++
我改为swift 3代码发生"一元运算符'++'不能应用于'@lvalue String.Index'类型的操作数(又名'@lvalue String.CharacterView.Index')"在swift 3中
我重写了例如.i + = 1
但是这段代码无法解决.请帮我.
我想使用反射从objective-c类中获取变量值.
company.h看起来像
@interface Company : NSObject {
}
@property (nonatomic, retain) NSString* phone;
- (NSString*) getPropertyValueByName: (NSString*) paramName;
Run Code Online (Sandbox Code Playgroud)
company.m看起来像
@implementation Company
@synthesize phone;
- (NSString*) getPropertyValueByName: (NSString*) paramName
{
id me = self;
value = [me objectForKey: paramName];
return value;
}
@end
Run Code Online (Sandbox Code Playgroud)
我这样使用:
Company* comp = [Company new];
comp.phone = @"+30123456";
NSString* phone = comp.getPropertyValueByName(@"phone");
Run Code Online (Sandbox Code Playgroud)
我得到的是一个例外:
-[Company objectForKey:]: unrecognized selector sent to instance
Run Code Online (Sandbox Code Playgroud) 我们目前正忙于从Visual Studio 2005迁移到Visual Studio 2010(使用非托管C/C++).这意味着我们大约一半的开发人员已经在使用Visual Studio 2010,而另一半仍在使用Visual Studio 2005.最近,我遇到了一种情况,在Visual Studio 2010中可以以干净的方式编写某种构造,但是在Visual Studio 2005中需要不太干净的源代码.因为并非所有开发人员都在其计算机上安装了Visual Studio 2010,所以我必须编写如下代码:
#if _MSC_VER >= 1600
// clean version of the source code
#else
// less clean version
// of the source code
// requiring multiple lines of code
// and requiring some dirty static_casts
#endif
Run Code Online (Sandbox Code Playgroud)
由于所有开发人员将在今年年底之前迁移到Visual Studio 2010,因此我希望此代码在某个时刻后自动"消失".在源代码中保持"不太干净的版本"会导致长期不可读的源代码.
当然,我知道代码不会自动消失,所以我真的想在一定时刻后自动响铃.像这样的东西:
#if _MSC_VER >= 1600
// clean version of the source code
#else
// less clean version
// of the source code
// requiring multiple lines of code …Run Code Online (Sandbox Code Playgroud) 当我阅读nginx代码时,我看到了这个函数:
#define ngx_cpymem(dst, src, n) (((u_char *) memcpy(dst, src, n)) + (n))
static ngx_inline u_char *
ngx_copy(u_char *dst, u_char *src, size_t len)
{
if (len < 17) {
while (len) {
*dst++ = *src++;
len--;
}
return dst;
} else {
return ngx_cpymem(dst, src, len);
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个简单的字符串复制功能.但是为什么它测试字符串的长度并在长度> = 17时切换到memcpy?