我正在努力学习线程,我很困惑.我确信所有的答案都在苹果文档中,但我发现很难分解和消化.也许有人可以为我清除一件事或两件事.
1)performSelectorOnMainThread
上面只是在主运行循环中注册一个事件,或者它是否以某种方式成为新线程,即使该方法显示"mainThread"?如果线程的目的是减轻主线程的处理,这有什么帮助?
2)RunLoops
如果我想创建一个完全独立的线程,我使用"detachNewThreadSelector"是真的吗?调用start on this会为已创建的线程启动默认运行循环吗?如果是这样,运行循环进入哪里?
3)最后,我看到了使用NSOperationQueue的例子.是不是说如果使用performSelectorOnMainThread,那么线程仍在队列中,所以不需要NSOperation?
4)我应该忘记所有这些而只是使用Grand Central Dispatch吗?
我正在尝试在c中创建一个可变大小的数组.
数组继续返回,其值为-1.
我想要做的是创建一个大小的数组,size然后逐步添加值.我究竟做错了什么?
int size = 4546548;
UInt32 ar[size];
//soundStructArray[audioFile].audioData = (UInt32 *)malloc(sizeof(UInt32) * totalFramesInFile);
//ar=(UInt32 *)malloc(sizeof(UInt32) * totalFramesInFile);
for (int b = 0; b < size; b++)
{
UInt32 l = soundStructArray[audioFile].audioDataLeft[b];
UInt32 r = soundStructArray[audioFile].audioDataRight[b];
UInt32 t = l+r;
ar[b] = t;
}
Run Code Online (Sandbox Code Playgroud) 我开始学习C中结构的使用.它具有挑战性和乐趣.不用说我遇到了一个我似乎无法弄清楚的问题.我正在尝试将灵活的struct数组作为另一个struct的成员,但我收到一个错误:
无效使用灵活数组
我究竟做错了什么?
#define NUM_CHANNELS 4
struct channelStruct {
float volume;
uint mute;
};
struct enginestruct
{
float bpm;
int synctimeinbeats;
int synctimeinsamples;
int currentbeat;
int oneBeatInSamples;
int samplerate;
struct channelStruct channels[];
};
struct enginestruct engine, *engineptr;
struct channelStruct channel, *channelptr;
-(void) setupengine
{
engineptr = &engine;
engineptr->oneBeatInSamples = 22050;
engineptr->samplerate = 44100;
struct channelStruct *ch = (struct channelStruct *) malloc (
NUM_CHANNELS*sizeof(struct channelStruct) );
//error occurs here
engineptr->channels = ch;
}
Run Code Online (Sandbox Code Playgroud)
编辑1
这是我想要达到的目标
编辑2*
好的,所以我似乎正在接近以错误的方式创建一个可变大小的struct数组.我有两件事正在尝试.我知道的第一个肯定是有效的.第二个我愿意,如果有人能够理智地检查它.我还在学习指针,想知道A是否与B相同.B是我的首选方法,但我不知道它是否正确.我对a有信心,因为当我调试频道时,我看到频道[0],频道[1]频道[2]等.但我对B不太自信,因为当我调试它时我只看到一个地址到内存和列出的通道结构的变量.
一个
// pretty …Run Code Online (Sandbox Code Playgroud) 重写这个问题会更有吸引力.
我的问题是我无法从远程IO单元成功地将音频文件写入磁盘.
我采取的步骤是
打开一个mp3文件并将其音频提取到缓冲区.我根据图表的属性设置了一个asbd用于我的图形.我设置并运行我的图形循环提取的音频和声音成功地从扬声器出来!
我遇到的困难是从远程IO回调中获取音频样本并将其写入磁盘上的音频文件,我正在使用ExtAudioFileWriteASync.
音频文件确实被写入并且与原始mp3有一些可听见的相似之处,但听起来非常扭曲.
我不确定问题是否存在
A)ExtAudioFileWriteAsync无法像io单元回调提供的那样快速写入样本.
B)我已经为extaudiofile refeference设置了ASBD错误.我想先保存一个wav文件.我不确定我是否在下面的ASBD中正确描述了这一点.
其次,我不确定在创建音频文件时为inChannelLayout属性传递什么值.
最后我非常不确定要用于kExtAudioFileProperty_ClientDataFormat的asbd.我一直在使用我的立体声流格式,但仔细看看文档说这必须是pcm.它应该与remoteio的输出格式相同吗?如果是这样,我错误地将远程io的输出格式设置为stereostreamformat?
我意识到这个问题有很多,但我有很多不确定因素,我似乎无法自行解决.
设置立体声流格式
- (void) setupStereoStreamFormat
{
size_t bytesPerSample = sizeof (AudioUnitSampleType);
stereoStreamFormat.mFormatID = kAudioFormatLinearPCM;
stereoStreamFormat.mFormatFlags = kAudioFormatFlagsAudioUnitCanonical;
stereoStreamFormat.mBytesPerPacket = bytesPerSample;
stereoStreamFormat.mFramesPerPacket = 1;
stereoStreamFormat.mBytesPerFrame = bytesPerSample;
stereoStreamFormat.mChannelsPerFrame = 2; // 2 indicates stereo
stereoStreamFormat.mBitsPerChannel = 8 * bytesPerSample;
stereoStreamFormat.mSampleRate = engineDescribtion.samplerate;
NSLog (@"The stereo stereo format :");
}
Run Code Online (Sandbox Code Playgroud)
使用立体声流格式设置remoteio回调
AudioUnitSetProperty(engineDescribtion.masterChannelMixerUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output,
masterChannelMixerUnitloop,
&stereoStreamFormat,
sizeof(stereoStreamFormat));
AudioUnitSetProperty(engineDescribtion.masterChannelMixerUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
masterChannelMixerUnitloop,
&stereoStreamFormat,
sizeof(stereoStreamFormat));
static OSStatus masterChannelMixerUnitCallback(void *inRefCon,
AudioUnitRenderActionFlags …Run Code Online (Sandbox Code Playgroud) 有人可以告诉我如何在可可中获得UIButton的x和y位置吗?
单击时按钮在屏幕上移动,我想使用它的当前位置来定位相对于它的另一个UIBUtton.
我试过用了
CGRect rect=[myButton imageRectForContentRect];
Run Code Online (Sandbox Code Playgroud)
编译器告诉我myButton可能不响应imageRectForContentRect.
我正在使用一些使用CGPath绘制弧的示例代码.我已经有大约一看,发现文档,但我似乎无法在我的脑海想象什么事情在使用MoveToPoint,AddLineToPoint等方面,我不能"看"是什么代码正在做我可以看到结果.
例如,下面的代码从3点钟位置开始绘制一个完整的360度弧.对于我的生活,我无法弄清楚如何让它从12点钟位置开始实际旋转视图 - 90度.
有人可以帮我弄清楚这个代码以及如何改变它以实现从12点开始,最好是试图解释整个路径是如何工作的.或者也许可以在线指出我的视觉资源?
- (void)drawPathWithArc:(CGFloat)arc {
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath, NULL, 100.f, 100.f);
CGPathAddLineToPoint(thePath, NULL, 200.f, 100.f);
CGPathAddArc(thePath, NULL, 100.f, 100.f, 100.f, 0.f, (360* M_PI)/180, NO);
CGPathCloseSubpath(thePath);
shapeLayer_.path = thePath;
CGPathRelease(thePath);
}
Run Code Online (Sandbox Code Playgroud) 我试图打开L2Cap HID通道和中断通道,以便我可以将HID命令发送到蓝牙设备.
我已完成所有服务广告和设备配对并建立了基带连接.
Hid控制通道打开正常.当我尝试创建我的kBluetoothL2CAPPSMHIDInterrupt连接时
l2capChannelQueueSpaceAvailable
委托方法调用(不确定这意味着什么)后跟
l2capChannelOpenComplete
但连接立即关闭
l2capChannelClosed
我该如何正确打开这些连接?
我花了很长时间挖掘IOBlueTooth框架和bluetooth.org HID规范,但几乎没有任何有用的信息(我至少可以找到).
当我追踪我的L2Cap频道时,我看到了一些空值
mIncomingDataListener
和
mEventDataListener
.我不知道如何设置它们或者它们是否与我的问题有关....只是推测.
下面的代码片段是我到目前为止在建立与设备的连接后建立连接的尝试.
-(void)establishL2CappConnections:(IOBluetoothDevice*)device
{
IOReturn r;
IOBluetoothL2CAPChannel *ch1;
r = [device openL2CAPChannelSync:&ch1
withPSM:(BluetoothL2CAPPSM)kBluetoothL2CAPPSMHIDControl
delegate:self];
self.mL2CappChannel=ch1;
NSLog(@"r == %i",r);
IOBluetoothL2CAPChannel *ch2;
r = [device openL2CAPChannelSync:&ch2
withPSM:(BluetoothL2CAPPSM)kBluetoothL2CAPPSMHIDInterrupt
delegate:self];
self.mL2CappInterruptChannel=ch2;
NSLog(@"r == %i",r);
}
Run Code Online (Sandbox Code Playgroud)
编辑:1
我已经附上了我的包日志.这很奇怪,是一个请求
kBluetoothL2CAPPSMSDP 0x0001
没有我请求它,然后一切开始断开连接.
我刚刚开始使用android,我的java非常生锈.我记不起曾经见过像这样嵌套在另一个函数中的函数.有人可以向我解释究竟是什么,并解释为什么你会在这样的另一个函数中嵌套函数?
private final Handler handler = new Handler() {
@Override
public void handleMessage(final Message msg) {
Log.v(Constants.LOGTAG, " " + ReviewList.CLASSTAG + " worker thread done, setup ReviewAdapter");
progressDialog.dismiss();
if ((reviews == null) || (reviews.size() == 0)) {
empty.setText("No Data");
} else {
reviewAdapter = new ReviewAdapter(ReviewList.this, reviews);
setListAdapter(reviewAdapter);
}
}
};
Run Code Online (Sandbox Code Playgroud) 我想知道在代码中使用未使用的函数会产生什么后果?
如果你追捕并删除所有未使用的函数和变量,那么性能会有任何令人难以置信的改善吗?
或者只是删除未使用的函数和变量的好习惯?
使用单词single可能会在标题中使用我的术语.我现在正在寻找一种好的技术.我有一个名为user的实体,用于存储登录数据的用户,例如用于发出服务器请求的会话密钥.我只想要其中一个实体存在.这样做有标准的技术吗?
到目前为止我所拥有的是这样的
NSManagedObjectContext *moc = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"UserEntity" inManagedObjectContext:moc];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];
NSArray *array = [moc executeFetchRequest:request error:&error];
if (array == nil)
{
// Deal with error...
}
if ([array count]==0) {
//first run of app
}else if([array count]==1)
{
// id like the code to enter here after every app run except for the first one
}else
{
//dont want this to happen
}
Run Code Online (Sandbox Code Playgroud) iphone ×4
c ×2
android ×1
arrays ×1
audiounit ×1
bluetooth ×1
cocoa ×1
cocoa-touch ×1
core-audio ×1
core-data ×1
iobluetooth ×1
ios ×1
java ×1
l2cap ×1
macos ×1
objective-c ×1
performance ×1
struct ×1
uibutton ×1