我正在使用雪豹.我想创建一个软件串口,它将显示为/ dev/tty ....
我想使用这个虚拟串口从各种程序和IDE中收集数据,如arduino和处理.
谁能告诉我如何做到这一点.我搜索了这个问题,我看过像netfwd这样的软件,但它们只提供串口重定向.
我是osx的新手,之前没有使用过linux.
我想定义一个协议并创建一种简单、标准的方法来获取所述协议的“默认”共享实现 - 单例样式。Cocoa 遵循以下模式:
[NSUserDefaults standardUserDefaults]
[NSNotificationCenter defaultCenter]
Run Code Online (Sandbox Code Playgroud)
但在这两种情况下,它们在对象层次结构的底部都有@interfaces。我正在努力解决如何使用@protocols 来做到这一点。显然,我可以创建一个具有空或简单方法实现的类 - 但实际上,我想要的是层次结构底部的 @protocol。我尝试过类似的事情:
@protocol ConfigurationManager <NSObject>
//...
@interface ConfigurationManagerFactory : NSObject
+ (id<ConfigurationManager>)sharedConfiguration;
@end
// ...
id<ConfigurationManger> config = [ConfigurationManagerFactory sharedConfiguration];
[config ...];
Run Code Online (Sandbox Code Playgroud)
它有效 - 但我总是必须解释如何使用它以及为什么我这样做。有没有一种方法可以符合 Cocoa 的语法(调用约定),同时仍然利用 @protocols 的价值?
顺便说一句,我是否有理由不想像这样使用@protocols?实现 @interface 仍然可以利用类别和替代实现等 - 就像实例化 NSString 通常会留下一个扩展 NSString 的类一样。
所以基本上我正在使用window.location ="myobj:mymethod:myarg:myotherarg"来实现在objc中处理JavaScript调用的典型方法,但是,我想知道是否有办法将一个参数数组应用于一个方法,类似于你在JavaScript中的方式.
通常我一直在做
-(void) mymethod:(NSArray*) arr{
//method knows how many arguments it takes and what they mean at each index
}
Run Code Online (Sandbox Code Playgroud)
我更愿意这样做:
-(void) mymethod:(NSString*) myarg myOtherArg: (NSString*) myotherarg{
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
并有一个这样的方法:
+(void) callMethod:(NSString*)selectorName withArgs: (NSArray*)args onObject:(id) obj{
//implementation
}
[JEHelpers callMethod:selector withArgs:someArrayOfArgs onObject:myapp]
Run Code Online (Sandbox Code Playgroud)
这可能吗?
我一直在网上搜索这个答案,但到目前为止还没有运气.所以我需要在这里咨询聪明善良的人.这是我第一次在这里提问,所以我希望我这样做是正确的而不是重复这个问题.
对于我看到的所有示例,它是搜索字符串,它是存储在Core Data中的子字符串.另一方面,我想实现以下目标:
存储在核心数据中的字符串实际上是子字符串.我想通过获取具有子字符串的所有核心数据行属于提供的搜索字符串来进行搜索.
例如:在核心数据中,我有"AB","BC","ABC","ABCDEF","GH","ABA"在应用程序中我通过提供超级字符串进行搜索:"ABCDEF" ,结果将返回"AB","BC","ABC","ABCDEF"但不返回"GH","ABA",因为这两个子字符串不属于超级字符串.
我应该如何设置我的predicateWithFormat语句?
这不会起作用,因为它正在做相反的事情:
NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"substring LIKE[c] %@", @"ABCDEF"];
Run Code Online (Sandbox Code Playgroud)
谢谢大家!
我有一个在Objective-C中定义的类,其初始化程序是-initWithError:(初始化程序可能由于依赖外部资源而失败).我希望这能成为Swift的桥梁init() throws.从NSObject继承的常规初始化程序-init可以标记为不可用,因为我不希望它被使用.
在Objective-C中,我有:
@interface Foo : NSObject
- (instancetype _Nullable)initWithError:(NSError **)error;
@end
Run Code Online (Sandbox Code Playgroud)
当然,这在Objective-C中工作正常.
在Swift中,-initWithError被桥接为init(error: ()) throws.这可能是因为withError:从方法名称中删除会导致init()与继承的普通-init初始化程序发生冲突.这可以从Swift调用,如下所示:
let foo = try Foo(error: ())
Run Code Online (Sandbox Code Playgroud)
这看起来很奇怪,因为error参数是无效的.如果将其作为进口进口肯定会更好init() throws.显而易见的解决方案是在Objective-C标头中标记-init使用NS_UNAVAILABLE.不幸的是,这不起作用.-initWithError:仍然被桥接init(error: ()),并试图try Foo()在编译器错误中调用结果,说明init()在Swift中不可用.
是否有一个更优雅的解决方案,这样try init()才有效?
initialization objective-c ios swift objective-c-swift-bridge
我试图用filterUsingPredicate来剔除我的NSMutableArray元素.
棘手的部分是我总是希望在数组中保留至少10个项目,无论它们是否通过谓词测试.所以类似于:
NSPredicate * cullPredicate = [NSPredicate predicateWithFormat:
@"distanceFromCurrent <= 25 OR index < 10"];
[self.myArray filterUsingPredicate:cullPredicate];
Run Code Online (Sandbox Code Playgroud)
'OR指数<10'表示我想做什么......我意识到它不起作用.
这可能与谓词有关吗?或者我是否需要拉出另一个数组,其元素从10开始,应用过滤器,然后将其添加回原始数组?
我正在尝试使用新的支持在iOS 7中进行更复杂的复数本地化.我创建了一个.stringsdict文件,根据Foundation发行说明(以及Cocoa WWDC会话中的新功能)中的信息进行格式化.我已经验证.stringsdict正被复制到我的应用程序包中(确实-[NSBundle pathForResource:...]找到它).但是,+[NSString localizedStringWithFormat:]不会返回根据配置字典中的规则格式化的字符串.
码:
- (IBAction)textFieldEditingDidEnd:(UITextField *)sender
{
NSInteger numPeople = [sender.text integerValue];
self.textView.text = [NSString localizedStringWithFormat:
NSLocalizedString(@"%d people are in the room", @"%d people are in the room"), (long)numPeople];
}
Run Code Online (Sandbox Code Playgroud)
Localizable.stringsdict:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>%d people are in the room</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@num_people_in_room@ in the room</string>
<key>num_people_in_room</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>d</string>
<key>zero</key>
<string>No one is</string>
<key>one</key>
<string>A person is</string>
<key>two</key>
<string>Two people …Run Code Online (Sandbox Code Playgroud) 我试图在进行一些处理后写出音频文件,但收到错误。我已将错误减少到这个简单的独立案例:
import Foundation
import AVFoundation
do {
let inputFileURL = URL(fileURLWithPath: "/Users/andrewmadsen/Desktop/test.m4a")
let file = try AVAudioFile(forReading: inputFileURL, commonFormat: .pcmFormatFloat32, interleaved: true)
guard let buffer = AVAudioPCMBuffer(pcmFormat: file.processingFormat, frameCapacity: AVAudioFrameCount(file.length)) else {
throw NSError()
}
buffer.frameLength = buffer.frameCapacity
try file.read(into: buffer)
let tempURL =
URL(fileURLWithPath: NSTemporaryDirectory())
.appendingPathComponent("com.openreelsoftware.AudioWriteTest")
.appendingPathComponent(UUID().uuidString)
.appendingPathExtension("caf")
let fm = FileManager.default
let dirURL = tempURL.deletingLastPathComponent()
if !fm.fileExists(atPath: dirURL.path, isDirectory: nil) {
try fm.createDirectory(at: dirURL, withIntermediateDirectories: true, attributes: nil)
}
var settings = buffer.format.settings
settings[AVAudioFileTypeKey] = kAudioFileCAFType
let tempFile …Run Code Online (Sandbox Code Playgroud) 我是编程,objective-c(和stackoverflow)的新手.我正在慢慢地学习和前进;)然后我遇到了一个谷歌无法解决的问题.我有一个窗口和一个NSview,然后添加了一个鼠标事件,应该将坐标绘制到我的视图中,但事实并非如此.有趣的是:当鼠标移动到我的应用程序窗口的窗口按钮时绘制它...
- (void)drawRect:(NSRect)dirtyRect {
NSPoint imagePos = NSMakePoint(0, 0);
NSImage *aImage = [NSImage imageNamed:@"mw_bg01.png"];
[aImage dissolveToPoint:imagePos fraction:1.0];
}
- (void)mouseDown:(NSEvent*)theEvent;{
mouseLoc = [NSEvent mouseLocation];
mousePosX = mouseLoc.x;mousePosY = mouseLoc.y;
NSString* mouseString = [NSString stringWithFormat:@"%d", mousePosX];
NSPoint textPoint = NSMakePoint(5, 5);
NSMutableDictionary *textAttrib = [[NSMutableDictionary alloc] init];
[textAttrib setObject:[NSFont fontWithName:@"Helvetica Light" size:10]
forKey:NSFontAttributeName];
[textAttrib setObject:[NSColor grayColor] forKey:NSForegroundColorAttributeName];
[mouseString drawAtPoint:textPoint withAttributes:textAttrib];
}
Run Code Online (Sandbox Code Playgroud)
我不知道怎么回事,有什么建议吗?谢谢!
我有一个VStack带 3Text的。每个都有不同长度的字符串。我希望 的VStack宽度足够大以适合最宽的Text,但我也希望所有三个Texts 水平填充 VStack 。
默认情况下,使用以下代码:
VStack(spacing: 4.0) {
ForEach(1..<4) {
Text(Array<String>(repeating: "word", count: $0).joined(separator: " "))
.background(Color.gray)
}
}
Run Code Online (Sandbox Code Playgroud)
我得到:
我想:
在 UIKit 中,我可以使用属性设置为 的UIStackView垂直线来执行此操作。没有对齐。我见过的建议解决方案是使用for修改堆栈视图的每个子视图(即每个)的框架。alignment.fillVStack.fillText.infinitymaxWidth
我发现的建议是修改Texts .frame(maxWidth: .infinity)。然而,这使得整体VStack扩展至(大概)其最大尺寸:
有没有办法让VStack自然生长到其最宽子级的大小,而不是更大,同时使其所有子级宽度相同?
objective-c ×7
ios ×6
cocoa ×3
macos ×3
swift ×3
nspredicate ×2
avfoundation ×1
core-data ×1
javascript ×1
localization ×1
mouseevent ×1
nsview ×1
serial-port ×1
singleton ×1
swiftui ×1
vstack ×1
xcode ×1